Java Programming
Java Programming
COMPUTER
PRACTICAL
FILE
SUBMITTED TO: SUBMITTED BY:
Mr. Vijay Baretha Shreya Dhawan
Class : 9C
Roll No: 37
ACKNOWLEDGEMENT
1
To print the perimeter and area of a square using assignment
statement.
3
To swap two numbers using third variable using assignment
statement.
4
Toconvert minutes to hours using Input through parameters.
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.
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
OUTPUT
PROGRAM 2
Write a program in java to convert temperature from
Fahrenheit to Celsius using assignment statement.
PROGRAM
public class Temperature
double F;
F=81;
}
VARIABLE DESCRIPTION
OUTPUT
PROGRAM 3
Write a program in java to swap two numbers using third variable
using assignment statement.
PROGRAM
public class Swap_Numbers
System.out.println("--Before swap--");
int temp = a;
a = b;
b = temp;
System.out.println("--After swap--");
}
VARIABLE DESCRIPTION
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;
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);
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;
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( );
VARIABLE DESCRIPTION
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;
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;
{
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 :
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 :
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 :
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;
}