0% found this document useful (0 votes)
23 views8 pages

Class 9 lab Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views8 pages

Class 9 lab Manual

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

1. Program to find the area and perimeter of a rectangle.

public class Rectangle {


public static void main (String args[]) {
int l ,b, area, perimeter;
l = 5;
b = 4;
area = l * b;
perimeter = 2*(l+b);
System.out.println("Area of Rectangle is: "+area);
System.out.println("Perimeter of Rectangle is: "+perimeter);
} }

Variable description:
Variable Name Data Type Description
l int Length
b int Breadth
area int Area of rectangle
perimeter int Perimeter of rectangle

2. Program to find the factorial of a given number.

import java.util.*;
public class Factorial {
public static void main(String args[])
{
int i, number,fact=1;
Scanner sc = new Scanner(System.in);
System.out.print(" Please Enter any Number : "); //It is the number to calculate factorial
number = sc.nextInt();
for( i=1; i<=number; i++)
{
fact=fact * i ;
}
System.out.println("Factorial of "+number+" is: "+fact);
} }

Variable description:

Variable Name Data Type Description


i int Loop
number int Positive number
fact int Factorial of the number
3. Program to find the square of a number.

import java.util.*;
public class SquareofNumber {
public static void main(String[] args)
{
int number, square;
Scanner sc = new Scanner(System.in);
System.out.print(" Please Enter any Number : ");
number = sc.nextInt();
square = number * number;
System.out.println("\n The Square of a Given Number " + number + " = " + square);
} }

Variable description:

Variable Name Data Type Description


number int Integer
square int Square of the integer

4. Program to assign four integers and print their average.

import java.util.*;
public class Average {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
// Prompt the user to enter four integers
System.out.println("Enter four integers: ");
int num1 = sc.nextInt();
int num2 = sc.nextInt();
int num3 = sc.nextInt();
int num4 = sc.nextInt();
double average = (num1 + num2 + num3 + num4) / 4;
System.out.println("The average of the four integers is: " + average);
} }

Variable description:

Variable Name Data Type Description


num1 int First integer
num2 int Second integer
num3 int Third integer
num4 int Fourth integer
average double Average of four integer
5. Program to Multiply Two Numbers.

import java.util.*;
public class MultiplyTwoNumbers {
public static void main(String[] args) {
int Num1, Num2, product;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the First integer Value = ");
Num1 = sc.nextInt();
System.out.print("Enter the Second integer Value = ");
Num2 = sc.nextInt();
product = Num1 * Num2;
System.out.println("\nProduct of the two integer values = " + product);
} }

Variable description:

Variable Name Data Type Description


Num1 int First integer
Num2 int Second integer
product int Product of two integers

6. Program using while loop to calculate the sum of first 100 natural numbers and
print their sum.

public class SumOfNaturalNumber {


public static void main(String[] args) {
int num = 100, i = 1, sum = 0;
while(i <= num) //executes until the condition returns true
{
sum = sum + i; //adding the value of i into sum variable
i++; //increments the value of me by 1
}
System.out.println("Sum of First 100 Natural Numbers is = " + sum);
}
}

Variable description:

Variable Name Data Type Description


num int First 100 natural numbers
i int while loop
sum int Sum of natural numbers
7. Program to calculate simple interest for any principal amount, with rate as 7.2%
and time as 3 years.

import java.util.*;
public class SimpleInterest {
public static void main (String args[]) {
double p, r = 7.2, t = 3, si; // principal amount, rate, time and simple interest respectively
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Principal amount : ");
p = sc.nextInt();
si = (p*r*t)/100;
System.out.println("Simple Interest is: " +si);
} }

Variable description:

Variable Name Data Type Description


p double Principal amount
r double Interest Rate
t double Years taken by principal
si double Simple Interest

8. Program to compute the circumference of a circle for a given value of radius.

import java.util.*;
public class CircumferenceOfCircle {
public static void main(String args[]) {
double circumference;
int radius;
Scanner sc=new Scanner (System.in); //object of the Scanner class
System.out.print("Enter the radius of the circle: ");
radius=sc.nextInt();
circumference=Math.PI*2*radius; //calculating circumference of circle
System.out.println("The circumference of the circle is: "+circumference); //prints the calcul
ated circumference
} }

Variable description:

Variable Name Data Type Description


radius int Radius of the circle
circumference double Circumference of the circle
9. Program to convert temperature 98.6 F into degree Celsius.

public class Celsius


{
public static void main (String args[ ])
{
double Fahrenheit, Celsius;
Fahrenheit = 98.6;
Celsius = ((Fahrenheit-32)*5)/9;
System.out.println("Temperature in celsius is: "+Celsius);
} }

Variable description:

Variable Name Data Type Description


Fahrenheit double Temperature in Fahrenheit
Celsius double Temperature in Celsius

10. Program to find whether the given number is Odd or Even.

import java.util.*;
public class EvenorOdd
{
public static void main(String[] args)
{
int Number;
Scanner sc = new Scanner(System.in);
System.out.println("\n Please Enter any integer Value: ");
Number = sc.nextInt();
if (Number % 2 == 0)
{
System.out.println("\n You have entered EVEN Number");
}
else
{
System.out.println("\n You have entered ODD Number");
} } }

Variable description:

Variable Name Data Type Description


Number int Number to be checked
11. Program to display right angle triangle pattern.

public class RightTrianglePattern


{
public static void main(String args[])
{
int i, j, row=5;
for(i=0; i<row; i++) //outer loop for rows
{
for(j=0; j<=i; j++) //inner loop for columns
{
System.out.print("* "); //prints stars
}
System.out.println(); //throws the cursor in a new line after printing each line
} } }

Variable description:

Variable Name Data Type Description


i int outer loop for rows
j int inner loop for columns
row int number of rows you want to print

12. Program to find Largest of Three Numbers.

import java.util.Scanner;
public class LargestNumber
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter First Number: ");
int a = in.nextInt();
System.out.print("Enter Second Number: ");
int b = in.nextInt();
System.out.print("Enter Third Number: ");
int c = in.nextInt();
int g = Math.max(a, b);
g = Math.max(g, c);
System.out.println("Largest Number = " + g);
} }
Variable description:

Variable Name Data Type Description


a int First number
b int Second number
c int Third number
g int Largest number

13. Program to find Smallest of Three Numbers.


import java.util.Scanner;
public class SmallestNumber
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter First Number: ");
int a = in.nextInt();
System.out.print("Enter Second Number: ");
int b = in.nextInt();
System.out.print("Enter Third Number: ");
int c = in.nextInt();
int s = Math.min(a, b);
s = Math.min(s, c);
System.out.println("Smallest Number = " + s);
} }

Variable description:

Variable Name Data Type Description


a int First number
b int Second number
c int Third number
s int Smallest number

14. Program to find the sum of 12.7 and 20.6. Also, subtract 20 from 23. Print
results.
public class SumAndDiff
{
public static void main(String args[])
{
int m1 = 23, m2 = 20, diff;
double n1 = 12.7, n2 = 20.6, sum;
sum = n1 + n2;
diff = m1 - m2;
System.out.println("The sum of numbers: "+sum);
System.out.println("The differences of numbers are: "+diff);
} }

Variable description:

Variable Name Data Type Description


n1 double First number
n2 double Second number
sum double Sum of first two numbers
m1 int First integer
m2 int Second integer
diff int Difference of two integers

15. Program to print Multiplication Table.

import java.util.Scanner;
public class MultiplicationTable
{
public static void main(String[] args)
{
int number, i;
Scanner sc = new Scanner(System.in);
System.out.print(" Please Enter any Number : ");
number = sc.nextInt();
{
for(i = 1; i <= 10; i++)
{
System.out.println(number +" * " + i + " = " + number * i);
}
}
}
}

Variable description:

Variable Name Data Type Description


number int Multiplication of the number
i int Loop

You might also like