Class 9 Comp Practical File 15 Programs 21-22 (2) Vijay Sir
Class 9 Comp Practical File 15 Programs 21-22 (2) Vijay Sir
Session: 2023-24
COMPUTER
PRACTICAL FILE
SUBMITTED TO: SUBMITTED BY:
Mr. Vijay Baretha Ansh Barman
Class : IX-A
Roll No: 13
Page 2 of 44
ACKNOWLEDGEMENT
CERTIFICATE
CONTENTS
No PROGRAM Page No.
To print the perimeter and area of a square using assignment
1 statement.
To convert temperature from Fahrenheit to Celsius using
2 assignment statement.
To swap two numbers using third variable using assignment
3 statement.
Toconvert minutes to hours using Input through parameters.
4
To calculate Simple Interest using Input through parameters.
5
To swap two numbers without using the third variable using input
6 through Scanner class.
To input two numbers and find larger and smaller numbers using
7 Mathematical methods input through Scanner class.
To determine if an entered number is a Buzz number or not.
8
To display the Fibonacci series between 1 and 100.
9
Menu driven program in java for calculating area of different
10 shapes and asks the user to calculate the area for that shape.
WAP to input a number and print all its factors.
11
WAP to input a number and check for prime number.
12
WAP to input two number and print its HCF (GCD).
13
WAP to input a number and for perfect number.
14
WAP to print the factorial of the number.
15
Page 5 of 44
PROGRAM 1
Write a program in java to print the perimeter and area of a
square using assignment statement.
PROGRAM
public class area_perimeter
{
VARIABLE DESCRIPTION
OUTPUT
Page 8 of 44
PROGRAM 2
Write a program in java to convert temperature from
Fahrenheit to Celsius using assignment statement.
PROGRAM
public class Temp
{
}
Page 9 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 10 of 44
z PROGRAM 3
Write a program in java to swap two numbers using third variable
using assignment statement.
PROGRAM
public class Swap
{
int n1 = 20, n2 = 45;
System.out.println("--Before swap--");
System.out.println("First number = " + n1);
System.out.println("Second number = " + n2); int temperature = n1;
n1 = n2;
n2 = temperature;
System.out.println("--After swap--");
System.out.println("First number = " + n1);
System.out.println("Second number = " + n2);
}
}
Page 11 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 13 of 44
PROGRAM 4
Write a program in java to convert minutes to hours using
Input through parameters.
PROGRAM
}
Page 14 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 15 of 44
PROGRAM 5
Write a program in java to calculate Simple Interest using
Input through parameters.
PROGRAM
float SI;
System.out.println("Principal is \u20B9 " + p);
System.out.println("Rate of Interest is = " + r + "% per annum");
System.out.println("Time is = " + t + " years");
SI=((p*r*t)/100);
VARIABLE DESCRIPTION
OUTPUT
Page 17 of 44
Page 18 of 44
PROGRAM 6
Write a program in java to swap two numbers without using
the third variable using input through Scanner class.
PROGRAM
import java.util.Scanner;
public class Swap_without_third_variable
{
}
}
Page 19 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 21 of 44
PROGRAM 7
Write a program in java to input two numbers and find
larger and smaller numbers using Mathematical methods
input through Scanner class.
PROGRAM
import java.util.Scanner;
public class Max_Min
{
public static void main(String args[ ])
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of first number: ");
double firstno = obj.nextDouble( );
System.out.println("Enter the value of second number: ");
double secondno = obj.nextDouble( );
System.out.println("Larger number is : " + Math.max(firstno,
secondno));
System.out.println("Smaller number is : " + Math.min(firstno,
secondno));
obj.close( );
}
Page 22 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 24 of 44
PROGRAM 8
Write a program in java to determine if an entered number
is a Buzz number or not.
PROGRAM
import java.util.Scanner;
public class Buzz_Num
{
public static void main(String args[ ])
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter a number: ");
int number = obj.nextInt( );
if ((number %10==7) || (number % 7==0))
System.out.println(number + " is a Buzz
number");else
System.out.println(number + " is not a Buzz
number");obj.close( );
}
}
Page 25 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 27 of 44
PROGRAM 9
Write a program in java to display the Fibonacci series
between 1 and 100.
PROGRAM
public class Fibonacci
{
public static void main(String args [])
{
int no1 = 0, no2= 1, sum= 0; System.out.print("Fibonacci
series between 1 and 100 is: "); System.out.print(no1 + " ");
System.out.print(no2 + " ");
sum = no1 + no2;
while (sum <= 100)
{
System.out.print(sum + " ");
no1=no2;
no2=sum;
sum = no1 + no2;
}
}
}
Page 28 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 30 of 44
PROGRAM 10
Write a Menu driven program in java for calculating area of
different shapes and asks the user to calculate the area for
that shape.
PROGRAM
import java.util.Scanner;
{
public static void main (String[] args)
float a,b,ar = 0;
Page 31 of 44
char choice;
//Declare input as scanner
Scanner input = new Scanner(System.in);
//Take inputs
System.out.println("Enter c for circle.");
System.out.println("Enter s for square.");
System.out.println("Enter r for rectangle.");
System.out.println("Enter t for triangle.");
String s = input.next();
choice = s.charAt(0);
//add a switch statement
switch(choice)
{
case 'c':
System.out.println("Enter radius:");
a = input.nextFloat();
ar = 3.14f*a*a;
break;
case 's':
System.out.println("Enter side:");
a = input.nextFloat();
ar = a*a;
break;
case 'r':
Page 32 of 44
a = input.nextFloat();
b = input.nextFloat();ar =
a*b;
break; case 't':
System.out.println("Enter base and height:");a
=input.nextFloat();
b =input.nextFloat();ar =
0.5f*a*b; break;
default:
System.out.println("Error");
}
System.out.println("Area = "+ar);
}
}
}
Page 33 of 44
VARIABLE DESCRIPTION
OUTPUT
Page 34 of 44
Page 35 of 44
PROGRAM 11
WAP to input a number and print all its factors.
import java.util.*;
class All_factors
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner (System.in);
int n,m;
System.out.println("Enter the number ");
m=sc.nextInt();
for(n=1;n<=m;n++)
{
if(m%n==0)
System.out.println(n)
;
}
}
}
Page 36 of 44
Output :
PROGRAM 12
WAP to input a number and check for prime number.
import java.util.*;
class
prime_number
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner (System.in);
int n,m,o=0;
System.out.println("Enter the number ");
m=sc.nextInt();
for(n=1;n<=m;n++)
{
if(m%n==
0)o++;
}
if(o==2)
System.out.println("prime number");
else
System.out.println(" not a prime number");
}
}
Page 38 of 44
Output :
PROGRAM 13
WAP to input two number and print its HCF (GCD).
import java.util.*;
class GCD
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner (System.in);
int n,m,l,min,hcf=0;
System.out.println("Enter the two number ");
m=sc.nextInt();
l=sc.nextInt();
min=Math.min(m,l);
for(n=1;n<=min;n++)
{
if(m%n==0 &&
l%n==0)hcf=n;
}
System.out.println("HCF="+hcf);
}
}
Page 40 of 44
Output :
PROGRAM 14
WAP to input a number and for perfect number.
Eg: n=6 it is a perfect number
import java.util.*;
class perfectnum
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner (System.in);
int x,y,z=0;
System.out.println("Enter the number ");
y=sc.nextInt();
for(x=1;x<y;x++)
{
if(y%x==0
)z=z+x;
}
if(z==y)
System.out.println("perfect number");
else
System.out.println(" not a perfect number");
}
}
Page 42 of 44
Output :
PROGRAM 15
WAP to print the factorial of the number.
Eg : 5!=120
import java.util.*;
class
factorial_of_num
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner (System.in);
int n,m,fact=1;
System.out.println("Enter the number ");
m=sc.nextInt();
for(n=1;n<=m;n++)
{
fact=fact*n;
}
Output :