0% found this document useful (0 votes)
11 views

Java Programming

Programs java

Uploaded by

shreyadhawan1511
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Java Programming

Programs java

Uploaded by

shreyadhawan1511
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 38

Session: 2022-23

COMPUTER
PRACTICAL
FILE
SUBMITTED TO: SUBMITTED BY:
Mr. Vijay Baretha Shreya Dhawan
Class : 9C
Roll No: 37
ACKNOWLEDGEMENT

I would like to thank my Computer Application


teacher “Mr. Vijay Baretha” for his able
guidance and support in completing my project.

I would also like to extend my gratitude to the


Principal “Rev. Fr. Alwyn Madtha” for providing
me with all the facilities that were required.

Date: Name: Shreya Dhawan


Class: 9C
CERTIFICATE

This is to certify that “Shreya Dhawan” of class 9C


of “St. Francis School, Lucknow has completed her
Computer practical file under my guidance. She has taken
proper care and shown utmost sincerity in completing
her Computer project.

I certify that this file is upto my expectations and as


per guidelines issued by submitted by ICSE.

Mr. Vijay Baretha


(Computer Teacher) Signature
CONTENTS
No PROGRAM Page No.

1
To print the perimeter and area of a square using assignment
statement.

2 To convert temperature from Fahrenheit to Celsius using


assignment statement.

3
To swap two numbers using third variable using assignment
statement.

4
Toconvert minutes to hours using Input through parameters.

To calculate Simple Interest using Input through parameters.


5
6
To swap two numbers without using the third variable using input
through Scanner class.

7 To input two numbers and find larger and smaller numbers using
Mathematical methods input through Scanner class.
To determine if an entered number is a Buzz number or not.
8
9
To display the Fibonacci series between 1 and 100.

10
Menu driven program in java for calculating area of
different shapes and asks the user to calculate the area for
that shape.

11
WAP to input a number and print all its factors.

WAP to input a number and check for prime number.


12
WAP to input two number and print its HCF (GCD).
13
14
WAP to input a number and for perfect number.

WAP to print the factorial of the number.


15
PROGRAM 1
Write a program in java to print the perimeter and area of a
square using assignment statement.

PROGRAM
public class Square
{
public static void main(String args[])
{
int a, P, A ;

a=50;
P = 4*a ;
A = a*a;
System.out.println("Side of the square is: " + a +" cm");
System.out.println("Perimeter of the square is: " + P +" cm");
System.out.println("Area of the square is: " + A +" square cm");
}
}
VARIABLE DESCRIPTION

No. Name Type Description


1 a int Input value
2. P int To calculate and store the value of perimeter of square
3. A int To calculate and store the value of area of square

OUTPUT
PROGRAM 2
Write a program in java to convert temperature from
Fahrenheit to Celsius using assignment statement.

PROGRAM
public class Temperature

public static void main(String[] args)

double F;

F=81;

System.out.println("Temperature in Fahrenheit is: " + F +" degree Fahrenheit");

double C =(( 5 *(F - 32.0)) / 9.0);

System.out.println(F+ " degree Fahrenheit is equal to " + C + " in Celsius");

}
VARIABLE DESCRIPTION

No. Name Type Description


1 F double Input value
2. C double To calculate and store the value in Celsius

OUTPUT
PROGRAM 3
Write a program in java to swap two numbers using third variable
using assignment statement.

PROGRAM
public class Swap_Numbers

public static void main(String[] args)

int a = 25, b = 55;

System.out.println("--Before swap--");

System.out.println("First number = " + a);

System.out.println("Second number = " + b);

int temp = a;

a = b;

b = temp;

System.out.println("--After swap--");

System.out.println("First number = " + a);

System.out.println("Second number = " + b);

}
VARIABLE DESCRIPTION

No. Name Data Type Description


1 a int Initial value of First number.
2. b int Initial value of Second number.
3. temp int Third variable used for Swapping.

OUTPUT
PROGRAM 4
Write a program in java to convert minutes to hours
using Input through parameters.

PROGRAM
public class MinutestoHours
{
public static void main(int totalMinutes)
{
int H, M; hours =
totalMinutes/60;
H = totalMinutes%60;

System.out.println("Entered minutes: " + totalMinutes);


System.out.println("There are " + H + " hours and " + M
+ " minutes in " + totalMinutes + " minutes");
}
}
VARIABLE DESCRIPTION

No. Name Type Description


1 totalMinutes int Input value of total number of minutes by the user.
2. H int To calculate and store the value in hours
3. M int To calculate and store the value in minutes

OUTPUT
PROGRAM 5
Write a program in java to calculate Simple Interest using
Input through parameters.

PROGRAM
public class Simple_Interest
{
public static void main (int principal,int rate,int time)
{
float SI;
System.out.println("Principal is \u20B9 " + principal); System.out.println("Rate
of Interest is = " + rate + "% per annum"); System.out.println("Time is = " +
time + " years"); SI=((principal*rate*time)/100);

System.out.println("Simple Interest is \u20B9 " + SI);


}
}
VARIABLE DESCRIPTION

No. Name Type Description


1 principal int Input value of Principal in Rs
2. rate int Input value of Rate of Interest
3. time int Input value of Time in Years
4. S float To calculate and store the value in si

OUTPUT
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_Num


{
public static void main(String args [ ])
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of First number: "); int
N1 = obj.nextInt();
System.out.println("Enter the value of Second number: "); int
N2 = obj.nextInt();
System.out.println("Before Swapping the numbers: "+"First number is "+N1
+" and "+"Second number is "+N2);
N1 = N1 + N2;
N2 = N1 – N2;
N1 = N1 – N2;
System.out.println("After swapping the numbers: "+"First number is "+N1 +"
and "+"Second number is " +N2);
obj.close();
}
}

VARIABLE DESCRIPTION
No. Name Data Description
Type
1 N1 int Input value of First number.
2. N2 int Input value of Second number.

OUTPUT
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
Find_Max_Min
{
public static void main(String args[ ])
{
Scanner obj = new Scanner(System.in);
System.out.println("Enter the value of first number: ");
double n1 = obj.nextDouble( );

System.out.println("Enter the value of second number: ");


double n2 = obj.nextDouble( );
System.out.println("Larger number is : " + Math.max(n1, n2));
System.out.println("Smaller number is : " + Math.min(n1, n2));
obj.close( );
}
}

VARIABLE DESCRIPTION

No. Name Data Description


Type
1 n1 double Input value of First number.
2. n2 double Input value of Second number.

OUTPUT
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_Number
{
public static void main(String args[ ])
{
Scanner obj = new
Scanner(System.in);
System.out.println("Enter a number: ");
int n = obj.nextInt( );
if ((n%10==7) || (n % 7==0))
System.out.println(n + " is a Buzz number");
else
System.out.println(n + " is not a Buzz number");
obj.close( );
}
}

VARIABLE DESCRIPTION
No. Name Data Description
Typ
e
1 n int Input value of number to be checked.

OUTPUT
PROGRAM 9
Write a program in java to display the Fibonacci
series between 1 and 100.

PROGRAM
public class Fibonacci_Series
{
public static void main(String args [])
{
int N1 = 0, N2= 1, S= 0;

System.out.print("Fibonacci series between 1 and 100 is: ");


System.out.print(N1 + " ");
System.out.print(N2 + " ");
S = N1 + N2;
while (S <= 100)
{
System.out.print(S + " ");
N1=N2;
N2=S;
S = N1 + N2;
}
}
}

VARIABLE DESCRIPTION
No. Name Data Description
Typ
e
1 N1 int First value of number for starting series
2 N2 int Second value of number for starting series
3 S int Adding values of preceding numbers

OUTPUT
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;

// the name of our class its public

public class Find_Area_MenuDriven

{
public static void main (String[] args)

float a,b,area = 0;
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();
area = 3.14f*a*a;
break;
case 's':
System.out.println("Enter
side:"); a = input.nextFloat();
area = a*a;
break;
case 'r':
System.out.println("Enter length and breadth:");
a = input.nextFloat();
b =
input.nextFloat();
area = a*b;
break;
case 't':
System.out.println("Enter base and height:");
a =input.nextFloat();
b =input.nextFloat();
area = 0.5f*a*b;
break;
default:
System.out.println("Error");
}
System.out.println("Area = "+area);
}
}
}

VARIABLE DESCRIPTION
No. Name Data Description
Typ
e
1 a float Input value length for rectangle /side for
square/radius for circle/ base for triangle
2 b float Input value breadth for rectangle/height for
triangle
3 area float To calculate and store the value of area
4. char choice Reading the choice of user based on area of
shape to be calculated
OUTPUT
PROGRAM 11
WAP to input a number and print all its factors.

import java.util.*;
class factors
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner
(System.in); int i,n;
System.out.println("Enter the number
"); n=sc.nextInt();
for(i=1;i<=n;i++)
{
if(n%i==0)
System.out.println(i);
}
}
}
Output :

Variable description table

Name Type Purpose


n Int To input the value
from the user.
i Int Loop index variable
PROGRAM 12
WAP to input a number and check for prime number.

import java.util.*;
class prime_no
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner
(System.in); int i,x,y=0;
System.out.println("Enter the number
"); x=sc.nextInt();
for(i=1;i<=x;i++)
{
if(x%i==0)
y++;
}
if(y==2)
System.out.println("prime number");
else
System.out.println(" not a prime number");
}
}
Output :

Variable description table

Name Type Purpose


x Int To input the value
from the user.
i Int Loop index variable

y Int Counter variable


PROGRAM 13
WAP to input two number and print its HCF (GCD).
import java.util.*;
class HCF
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner
(System.in); int i,x1,x2,min,h=0;
System.out.println("Enter the two number
"); x1=sc.nextInt();
x2=sc.nextInt();
min=Math.min(x1,x2);
for(i=1;i<=min;i++)
{
if(x1%i==0 &&
x2%i==0) h=i;
}
System.out.println("HCF="+h);
}
}
Output :

Variable description table

Name Type Purpose


x1 Int To input the value
from the user.
x1 Int To input the value
from the user.
i Int Loop index variable

h Int To store HCF


PROGRAM 14
WAP to input a number and for perfect number.
Eg: n=6 it is a perfect number

import java.util.*;
class perfect_no
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner
(System.in); int i,x,s=0;
System.out.println("Enter the number
"); x=sc.nextInt();
for(i=1;i<x;i++)
{
if(x%i==0)
s=s+i;
}
if(s==x)
System.out.println("perfect number");
else
System.out.println(" not a perfect number");
}
}
Output :

Variable description table

Name Type Purpose


x Int To input the value
from the user.
i Int Loop index variable

s Int To store sum of the


factors
PROGRAM 15
WAP to print the factorial of the number.
Eg : 5!=120

import java.util.*;
class factorial
{
public static void main(String agrs[] )
{
Scanner sc= new Scanner
(System.in); int i,x,f=1;
System.out.println("Enter the number
"); x=sc.nextInt();
for(i=1;i<=x;i++)
{
f=f*i;
}

System.out.println("The factorial of the number is="+f);


}
}
Output :

Variable description table

Name Type Purpose


x Int To input the value
from the user.
i Int Loop index variable

f Int To store factorial of


the number

You might also like