***Java Programs***
-By Sujal Mehra 9th D
ACKNOWLEDGEMENT
I would like to express my special gratitude to my
Computer Applications Teacher Mrs. Sagoreeka Pandey
for helping me complete this project and as well as our
Hon’ble Principal of LWSP Mrs. Aditi Banerjee for this
project.
I would like to thank my parents who have been a great
support and also provided help.
Thanking You,
Sujal Mehra
------------------------------------------------------------------------------------------
------------------------------------------------------------------------------------------
Table of Contents
S.No. Program’s details Page No.
1. Write a program to display “Welcome to the world of Java”.
2. Write a program to perform mathematical operations (+,-,*, /) on 50 and 20
3. Write a program to display the area of a circle whose radius is 7.
4. Write a program to find the area and perimeter of a rectangle where length = 6
and breadth =8.
5. Write a program to calculate perimeter and diagonal of a square. Take side as
an input.
6. Write a program to convert 3200 grams into Kg.
7. Write a program to find the simple interest and amount on Rs. 3000 at 5% for 2
years.
8. Write a program to convert Fahrenheit into Celsius. T(°C) = (T(°F) - 32) × 5/9
9. Write a program to convert number of 365 days into number of years, number
of months and number of days.
10. Write a program to input two unequal numbers. Display the numbers after
swapping their values in the variables without using a third variable.
11. Write a program to print the marks in three subjects of a student. Print the total
and percentage marks. Display the grade on the basis of following percentage.
Percentage Grade
Between 80 and 100 Grade A
Between 60 and 80 Grade B
Between 40 and 60 Grade C
Below 40 Grade D
12. Write a program to input a number and display the following ->
a) Logarithm of the entered number
b) Square root of the entered number
c) Cube root of the entered number
d) Round off the entered number
e) Absolute value of the given number
13. Write a program to input three angles of a triangle and check whether a triangle
is possible or not. If possible then check whether it is an acute-angled triangle,
right-angled or obtuse-angled triangle, otherwise, display “Triangle is not
possible”.
14. Write a program to input two unequal positive numbers and check whether
they are perfect square numbers or not. If the user enters a negative number
then the program displays the message “Square root of a negative number
can’t be determined.”
15. Write a program to accept a number and check whether the number is divisible
by 3 as well as 5. Otherwise, decide
a) If the number is divisible by 3 and not by 5.
b) If the number is divisible by 3 and not by 5.
c) If the number is neither divisible by 3 nor by 5.
16. A Pre-Paid taxi charges from the passenger as per the tariff given below:
Distance Rate
Up to 5 km ₹ 100
For the next 10 km ₹ 10/km
For the next 10 km ₹ 8/km
More than 25 km ₹ 5/km
Write a program to input the distance covered and calculate the amount paid
by the passenger. The program displays the printed bill with the details given
below:
Taxi No. :
Distance covered :
Amount :
17. Mr. Kumar is an LIC agent. He offers discount to his policy holders on the annual
premium. However, he also gets commission on the sum assured as per the
given tariff.
Sum Assured Discount Commission
Up to ₹ 1,00,000 5% 2%
₹ 1,00,001 and up to ₹ 2,00,000 8% 3%
₹ 2,00,001 and up to ₹ 5,00,000 10% 5%
More than ₹ 5,00,000 15% 7.5%
Write a program to input name of the policy holder, the sum assured and first
annual premium. Calculate the discount of the policy holder and the
commission of the agent. The program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :
18. The equivalent resistance of series and parallel connections of two resistances
are given by the formula:
(a) R1 = r1 + r2 (Series)
(b) R2 = (r1 * r2) / (r1 + r2) (Parallel)
Using a switch case statement, write a program to enter the value of r1 and r2.
Calculate and display the equivalent resistances accordingly.
19. Write a program to print the multiplication table of 12.
20. Write a program to input a number and count the number of digits. The
program further check whether it contains odd number of digit or even.
21. Write a program to input cost price and selling price of an article. If the selling
price is more than selling price than calculate the profit or profit percentage
otherwise calculate loss and loss percentage.
22. Write a menu driven program to accept a number from the user whether it is a
palindrome number or a perfect number.
23. Write a program in java to find the sum of the given series:
a) S = a2 + a2/2 + a2/3 + a2/4 + a2/5
b) S = 0+ 7 + 26 + 63
c) S = 1+1/2! + 1/3! + ¼! + 1/5!
24. Using Switch statement, write a menu driven program for the following:
i. To find and display the sum of the series given below:
S= x1 – x2 + x3 - x4 + x5 -x6 + …….. -x20
ii. To display the following series:
1, 11, 111, 1111, 11111,..
25. Write a program to input a number and check whether it is a ‘Spy number’ or
not
26. Write a program in BLUEJ to find the factorial of a number.
27. Write a program to find the sum of the following
S = 1! + 2! + 3! + 4!
28. Write a program in BLUEJ to display the following Fibonacci series:
1, 1, 2,3,5,8, 13, 21, 34
29. Write a program to generate the following patterns using iteration loops.
a) b)
1
#####
12
####
### 123
## 1234
# 12345
30. Write a program to print the following patterns.
a. * b. 1
** 21
*** 321
**** 4321
***** 54321
c. 5 5 5 5 5 d. 2
4444 24
333 246
22 24 68
1 2 4 6 8 10
*************
1) class p1
{
public static void main()
{
System.out.println("Welcome to the world of Java");
}
}
Variable Type Description
– – -
2) class p2
{
public static void main()
{
int a=50,b=20;
int Sum= a+b;
int Sub= a-b;
double Multi= a*b;
double Divide= a/b;
System.out.println(Sum);
System.out.println(Sub);
System.out.println(Multi);
System.out.println(Divide);
}
}
Variable Type Description
a Integer To store the value 50
b Integer To store the value 20
Sum Integer Addition of a and b
Sub Integer Subtraction of a and b
Multi Double Multiplication of a and b
Divide Double Division of a and b
3) public class p3
{
public static void main(String args[])
{
int r=7;
double Area= 3.14*r*r;
System.out.println("Area="+Area);
}
}
Variable Type Description
r Integer Radius of the Circle
Area Double Area of Circle
4) public class p4
{
public static void main()
{
int length=6;
int breadth=8;
double area=(length*breadth);
double perimeter=2*(length+breadth);
System.out.println("Area = "+area);
System.out.println("Perimeter = "+perimeter);
}
}
Variable Type Description
length Integer Length of Rectangle
breadth Integer Breadth of Rectangle
area Double Area of Rectangle
perimeter Double Perimeter of Rectangle
5) import java.util.Scanner;
public class p5
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the side of the square");
double side =sc.nextDouble();
double perimeter =4*side;
double diagonal =side*Math.sqrt(2);
System.out.println("Perimeter of the square : "+perimeter);
System.out.println("Length of the diagonal : "+diagonal);
}
}
Variable Type Description
side Double Side of Square
perimeter Double Perimeter of Square
diagonal Double Diagonal of Square
6) public class p6
{
public static void main()
{
double g = 3200;
double kg =g/1000;
System.out.println("Value of g in kg = "+kg);
}
}
Variable Type Description
g Double To store 3200 in grams
kg Double To store the value 3200 in
kilograms
7) public class p7
{
public static void main()
{
double P=3000;
double R=5;
double T=2;
double SI=(P*R*T)/100;
double A=P+SI;
System.out.println("Simple Interest = "+SI);
System.out.println("Amount = "+A);
}
}
Variable Type Description
P Double Principal
R Double Rate of Interest
T Double Time Period
SI Double Simple Interest
A Double Amount
8) public class p8
{
public static void main(double F)
{
double TC =(F-32)*5/9;
System.out.println("Value of Fahrenheit in Celsius = "+TC);
}
}
Variable Type Description
F Double Fahrenheit
TC Double Celsius
9) class p9
{
static void find()
{
int ndays=365;
int year, week, days;
int dweek = 7;
year = ndays / 365;
week = (ndays/dweek);
days = ndays/1;
System.out.println("years = " + year);
System.out.println("weeks = " + week);
System.out.println("days = " + days);
}
}
Variable Type Description
ndays Integer No. of days
year, week, days Integer To store the value in year,
week, days
dweek Integer Days in week
10) import java.util.Scanner;
public class p10
{
public static void main()
{
Scanner in = new Scanner(System.in);
System.out.println("Enter two unequal numbers");
System.out.print("Enter the first number: ");
int a = in.nextInt();
System.out.print("Enter the second number: ");
int b = in.nextInt();
a = a + b;
b = a - b;
a = a - b;
System.out.println("a = " + a + " b = " + b);
}
}
Variable Type Description
a Integer The First Number
b Integer The Second Number
11) import java.util.Scanner;
public class p11
{
public static void main(String args [])
{
Scanner sc = new Scanner(System.in);
char ch=' ';
System.out.println("Enter the marks in a,b and c subjects");
double a=sc.nextDouble();
double b=sc.nextDouble();
double c=sc.nextDouble();
double Total=a+b+c;
System.out.println("total marks obtained = "+ Total);
double percentage =(Total/300)*100;
if(percentage>=80&&percentage<=100)
ch='A';
if(percentage>=60&&percentage<80)
ch='B';
if(percentage>=40&&percentage<60)
ch='C';
if(percentage<40)
ch='D';
System.out.println("percentage = "+percentage);
System.out.println("Grade = "+ch);
}
}
Variable Type Description
ch Char To store char type of data/
Grade Obtained
a, b, c Double To enter marks in a,b,c
subjects
Total Double Total Marks Obtained
percentage Double Percentage Obtained
12) import java.util.Scanner;
public class p12
{
public static void main()
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number");
double n=sc.nextDouble();
System.out.println("Logarithm of the entered number = "+Math.log(n));
System.out.println("Square root of the entered number = "+Math.sqrt(n));
System.out.println("Cube root of the entered number = "+ Math.cbrt(n));
System.out.println("Round off the entered number = "+Math.round(n));
System.out.println("Absolute value of the given number = "+Math.abs(n));
}
}
Variable Type Description
n Double The Number Entered
13) import java.util.Scanner;
public class p13
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the first Angle: ");
int A1 = in.nextInt();
System.out.print("Enter the second Angle: ");
int A2 = in.nextInt();
System.out.print("Enter the third Angle: ");
int A3 = in.nextInt();
int AngleSum = A1 + A2 + A3;
if (AngleSum == 180 && A1 > 0 && A2 > 0 && A3 > 0)
{
if (A1 < 90 && A2 < 90 && A3 < 90)
{
System.out.println("Acute-Angled TriAngle");
}
else if (A1 == 90 || A2 == 90 || A3 == 90)
{
System.out.println("Right-Angled TriAngle");
}
else {
System.out.println("Obtuse-Angled TriAngle");
}
}
else
{
System.out.println("TriAngle is not possible");
}
}
}
Variable Type Description
A1, A2, A3 Integer First , Second and Third
Angle
AngleSum Integer Sum of the Angles of a
Triangle
14) import java.util.Scanner;
public class p14
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter the first number: ");
int a = in.nextInt();
System.out.print("Enter the second number: ");
int b = in.nextInt();
if (a < 0 || b < 0)
{
System.out.println("Square root of a negative number can't be determined");
}
else
{
double sqrtA = Math.sqrt(a);
double sqrtB = Math.sqrt(b);
double PsqA = sqrtA - Math.floor(sqrtA);
double Psqb = sqrtB - Math.floor(sqrtB);
if (PsqA == 0 && Psqb == 0)
{
System.out.println("They are perfect square numbers.");
}
else if (PsqA == 0)
{
System.out.println(a + " is a perfect square number.");
System.out.println(b + " is not a perfect square number.");
}
else if (Psqb == 0)
{
System.out.println(a + " is not a perfect square number.");
System.out.println(b + " is a perfect square number.");
}
else
{
System.out.println("Both are not perfect square numbers.");
}
}
}
}
Variable Type Description
a, b Integer First and Second Number
sqrtA, sqrtB Double Square Root of a and b
PsqA, PsqB Double Perfect Square Number
15) import java.util.Scanner;
public class p15
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter a number: ");
int num = in.nextInt();
if (num % 3 == 0 && num % 5 == 0)
System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}
Variable Type Description
num Integer The Number Entered
16) import java.util.Scanner;
public class p16
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Taxi Number: ");
String taxiNo = in.nextLine();
System.out.print("Enter distance travelled: ");
int dis = in.nextInt();
int fare = 0;
if (dis <= 5)
fare = 100;
else if (dis <= 15)
fare = 100 + (dis - 5) * 10;
else if (dis <= 25)
fare = 100 + 100 + (dis - 15) * 8;
else
fare = 100 + 100 + 80 + (dis - 25) * 5;
System.out.println("Taxi No: " + taxiNo);
System.out.println("Distance covered: " + dis);
System.out.println("Amount: " + fare);
}
}
Variable Type Description
taxiNo String Taxi Number
dis Integer Distance Travelled
fare Integer Total Amount
17) import java.util.Scanner;
public class p17
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Sum Assured: ");
double sum = in.nextDouble();
System.out.print("Enter First Premium: ");
double prem = in.nextDouble();
double dis = 0.0, comm = 0.0;
if(sum <= 100000){
dis = prem * 5.0 / 100.0;
comm = sum * 2.0 / 100.0;
}
else if(sum <= 200000){
dis = prem * 8.0 / 100.0;
comm = sum * 3.0 / 100.0;
}
else if(sum <= 500000){
dis = prem * 10.0 / 100.0;
comm = sum * 5.0 / 100.0;
}
else{
dis = prem * 15.0 / 100.0;
comm = sum * 7.5 / 100.0;
}
System.out.println("Name of the policy holder: " + name);
System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + prem);
System.out.println("Discount on the first premium: " + dis);
System.out.println("Commission of the agent: " + comm);
}
}
Variable Type Description
name String Name of the policy holder
sum Double Sum assured
prem Double Premium
dis Double Discount on the first
premium
comm Double Commission of the agent
18) import java.util.Scanner;
public class p18
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.println("1. Series");
System.out.println("2. Parallel");
System.out.print("Enter your choice: ");
int choice = in.nextInt();
boolean isChoiceValid = true;
System.out.print("Enter r1: ");
double r1 = in.nextDouble();
System.out.print("Enter r2: ");
double r2 = in.nextDouble();
double eqr = 0.0;
switch (choice) {
case 1:
eqr = r1 + r2;
break;
case 2:
eqr = (r1 * r2) / (r1 + r2);
break;
default:
isChoiceValid = false;
System.out.println("Incorrect choice");
break;
}
if (isChoiceValid)
System.out.println("Equivalent resistance = " + eqr);
}
}
Variable Type Description
choice Integer The choice of the user
isChoiceValid Boolean To check if condition is True
or False
r1, r2 Double To input Resistance 1 and
Resistance 2
eqr Double Equivalent resistance
19) public class p19
{
public static void main(String[] args)
{
int num = 12;
for(int i = 1; i <= 10; ++i)
{
System.out.println(num+" * "+i+" = "+num*i);
}
}
}
Variable Type Description
num Integer To store the the number 12
i Integer Counter Variable
20) import java.util.Scanner;
public class p20
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter number: ");
int num = sc.nextInt();
int ds = 0;
while (num != 0)
{
ds++;
num/= 10;
}
System.out.println("Number of digits = " + ds);
if (ds % 2 == 0)
System.out.println("Even number of digits");
else
System.out.println("Odd number of digits");
}
}
Variable Type Description
num Integer The Number Entered
ds Integer Number of digits
21) import java.util.Scanner;
public class p21
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter selling price: ");
double sp = sc.nextDouble();
System.out.print("Enter cost price: ");
double cp = sc.nextDouble();
double p = sp - cp;
double percent = Math.abs(p) / cp * 100;
if (p > 0)
{
System.out.println("Profit = " + p);
System.out.println("Profit % = " + percent);
}
else if (p < 0)
{
System.out.println("Loss = " + Math.abs(p));
System.out.println("Loss % = " + percent);
}
else
{
System.out.println("Neither profit nor loss");
}
}
}
Variable Type Description
sp Double Selling Price
cp Double Cost Price
p Double Profit
percent Double Percentage
22) import java.util.Scanner;
public class p22
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter your number: ");
int num = sc.nextInt();
System.out.println("Enter 1 for Palindrome number");
System.out.println("Enter 2 for Perfect number");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
int p = num;
int r = 0;
do
{
int d = num % 10;
r = r * 10 + d;
num = num / 10;
}
while(num!=0);
if (r == p)
System.out.println("Number is palindrome");
else
System.out.println("Number is not palindrome");
break;
case 2:
int sum = 0;
for (int i = 1; i <= num / 2; i++)
{
if (num % i == 0)
{
sum += i;
}
}
if (num == sum)
System.out.println(num + " is a perfect number");
else
System.out.println(num + " is not a perfect number");
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Variable Type Description
num Integer The Number Entered
ch Integer Choice of the User
p, r, d Integer To store integer type of data
i Integer Counter Variable
sum Integer To store the sum
23)a) import java.util.Scanner;
public class p23a
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number");
int a = sc.nextInt();
int sum = 0;
for (int i = 1; i <= 5; i++)
{
sum += (Math.pow(a,2)/i);
}
System.out.println("Sum:" +sum);
}
}
Variable Type Description
a Integer The Number Entered
i Integer Counter Variable
sum Integer The Sum of the Series
b) public class p23b
{
public static void main(String args[])
{
System.out.println("The sum of the series");
System.out.println("S = 0 + 7 + 26 + 63 is");
int sum = 0;
sum = 0+7+26+63;
System.out.println(""+sum);
}
}
Variable Type Description
sum Integer The Sum of the Series
c) public class p23c
{
public static void main(String args[])
{
double sum = 0.0;
for (int i = 1; i <= 5; i++)
{
long fact = 1;
for (int j = 1; j <= i; j++)
{
fact *= j;
}
sum += (1.0 / fact);
}
System.out.println("Sum=" + sum);
}
}
Variable Type Description
sum Double The Sum of the Series
i, j Integer Counter Variables
fact Long Factorial of the Number
24) import java.util.Scanner;
public class p24
{
public static void main(String args[])
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Sum of the series");
System.out.println("Enter 2 for Display series");
System.out.print("Enter your choice: ");
int ch = sc.nextInt();
switch (ch)
{
case 1:
System.out.print("Enter the value of x: ");
int x = sc.nextInt();
int sum = 0;
for (int i = 1; i <= 20; i++)
{
int term = (int)Math.pow(x, i);
if (i % 2 == 0)
sum -= term;
else
sum += term;
}
System.out.println("Sum=" + sum);
break;
case 2:
int term = 1;
for (int i = 1; i <= 5; i++)
{
System.out.print(term + " ");
term = term * 10 + 1;
}
break;
default:
System.out.println("Incorrect Choice");
break;
}
}
}
Variable Type Description
ch Integer To Enter the User’s Choice
x Integer To store the value entered by
the User
sum Integer To store the sum
i Integer Counter Variable
term Integer To store the integer type of
data in the variable term
25) import java.util.Scanner;
public class p25
{
public static void main(String args[])
{
int n,product=1,sum=0;
int a;
Scanner sc = new Scanner(System.in);
System.out.print("Enter the Number:");
n = sc.nextInt();
while (n > 0)
{
a = n % 10;
sum = sum + a;
product = product * a;
n = n / 10;
}
if (sum == product)
System.out.println("It is Spy Number");
else
System.out.println("It is not Spy Number");
}
}
Variable Type Description
n Integer The Number Entered
a Integer To store integer type of data
product Integer To store the product of the
number
sum Integer To store the sum of the
number
26) import java.util.Scanner;
public class p26
{
public static void main(String args[])
{
int i,n,fac=1;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number:");
n = sc.nextInt();
for( i = 1; i <= n; i++)
{
fac *= i;
}
System.out.print("The factorial of the number is "+fac);
}
}
Variable Type Description
i Integer Counter Variable
n Integer The Number Entered
fac Integer The Factorial of the Number
27) public class p27
{
public static void main(String args[])
{
int i,n=4,fac=1,sum=0;
for( i = 1; i <= n; i++)
{
fac *= i;
sum += fac;
}
System.out.print("The sum of the following is "+sum);
}
}
Variable Type Description
i Integer Counter Variable
n Integer To store the value 4 in the
variable n
fac Integer Factorial of the Number
sum Integer The Sum of the following
Series
28) public class p28
{
public static void main()
{
int a = 0;
int b = 0;
int c = 1;
for(int i=1;i<=10;i++)
{
a=b;
b=c;
c=a+b;
System.out.print(a+" ");
}
}
}
Variable Type Description
a, b, c Integer To store integer type of data
i Integer Counter Variable
29)a) public class p29a
{
public static void main(String args[])
{
for (int i = 1; i <=5; i++)
{
for (int j = 5; j >= i; j--)
{
System.out.print("#");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
b) public class p29b
{
public static void main(String args[])
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print(j+"");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
30)a) public class p30a
{
public static void main(String args[])
{
for (int i = 1; i <=5; i++)
{
for (int j = 1; j <= i; j++)
{
System.out.print("*"+" ");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
b) public class p30b
{
public static void main(String args[])
{
for (int i = 1; i <=5; i++)
{
for (int j = i; j >= 1; j--)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
c) public class p30c
{
public static void main(String args[])
{
for (int i = 5; i >=1; i--)
{
for (int j = 1; j <= i; j++)
{
System.out.print(i+" ");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
d) public class p30d
{
public static void main(String args[])
{
for (int i = 2; i <=10; i=i+2)
{
for (int j = 2; j <= i; j=j+2)
{
System.out.print(j+" ");
}
System.out.println();
}
}
}
Variable Type Description
i, j Integer Counter Variables
Bibliography-
★ ICSE IX COMPUTER APPLICATIONS BOOK
★ Knowledge Boat
★ Javatpoint