Divi Mehta Computer Practical File
Divi Mehta Computer Practical File
ON
“JAVA PROGRAMMING”
GRADE - 9
SESSION - (2024-2025)
Submitted To - Submitted By –
Sana Maqsood Divi Mehta 9C
ACKNOWLEDGEMENT –
1. Question 1 4-5
2. Question 2 6
3. Question 3 7
4. Question 4 8
5. Question 5 9
6. Question 6 10 - 11
7. Question 7 12
8. Question 8 13 - 14
9. Question 9 15 - 16
10. Question 10 17
11. Question 11 18 - 19
12. Question 12 20 - 21
13. Question 13 22 - 23
14. Question 14 24 - 25
15. Question 15 26 - 27
16. Question 16 28 - 30
17. Question 17 31 - 32
18. Question 18 33 - 34
19. Question 19 35 - 37
20. Question 20 38 - 39
Page | 3
Q1. Write a program to read the number n via the Scanner class and print the
Tribonacci series: 0, 0, 1, 1, 2, 4, 7, 13, 24, 44, 81 ...and so on. Hint: The
Tribonacci series is a generalization of the Fibonacci sequence where each term is
the sum of the three preceding terms.
Solution -
/*
* @author - xyz
* @purpose - printing the tribonacci series
*/
import java.util.*;
class TribonacciSeries
{
public static void main()
{
int i,a,b,c,d;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the ending value - "); //entering the ending
value for the loop
int n=sc.nextInt();
a=0;//initializing value
b=0;
c=1;
System.out.print(a+", ");
System.out.print(b+", ");
System.out.print(c+", ");
for(i=1;i<=n;i++)
{
d=a+b+c;
System.out.print(d+", "); //printing the Tribonacci series
a=b;
b=c;
c=d;
}
}
}
Page | 4
Variable Description
S.No Variable Data Type Scope Purpose
1. i int local Looping purpose
2. a int local Assigning value
3. b int local Assigning value
4. c int local Assigning value
Output –
Page | 5
Q2. In a competitive examination, there were 150 questions. One candidate got
80% correct and the other candidate 72% correct. Write a program to calculate
and display the correct answers each candidate got.
Solution -
/*
* @author - xyz
* @purpose – calculating the no. of correct answers
*/
class Exam
{
public static void main()
{
int ans1,ans2;
ans1=80*150/100; //calculating the correct answers by candidate 1
ans2=72*150/100; //calculating the correct answers by candidate 2
System.out.println("Correct answers got by candidate 1 are - "+ans1);
System.out.println("Correct answers got by candidate 2 are - "+ans2);
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. ans1 int local Calculating no. of correct answer
2. ans2 int local Calculating no. of correct answer
Output -
Page | 6
Q3. The normal temperature of the human body is 98.6°F. Write a programto
convert the temperature into degree Celsius and display the output.
Hint: c / 5 = f - 32 / 9
Solution -
/*
* @author-xyz
* @purpose-changing temperature from Fahrenheit to Celsius
*/
class Temperature
{
public static void main()
{
double c;
c=((98.6-32)/9)*5; //changing from F to C by applying formula
System.out.println("The body temperature in Celsius is "+c+"°C");
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. c double local Changing Fahrenheit to Celsius
Output –
Page | 7
Q4. The angles of a quadrilateral are in the ratio 3:4:5:6. Write a
program to find and display all its angles.
[Hint: The sum of angles of a quadrilateral = 360°]
Solution -
/*
* @author-xyz
* @purpose- writing a program to calculate the angles of a quadrilateral
*/
class Quad
{
public static void main()
{
int sum=0,x;
sum=3+4+5+6; //calculating the sum of sides in ratio
x=360/sum; //finding value of x
System.out.println("Angle 1 is - "+x*3+"°"); //printing the angles
System.out.println("Angle 2 is - "+x*4+"°");
System.out.println("Angle 3 is - "+x*5+"°");
System.out.println("Angle 4 is - "+x*6+"°");
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. sum int local Calculating the sum
2. x int local Calculating value
Output –
Page | 8
Q5. The time period of a Simple Pendulum is given by the formula: T = 2π√(l/g)
Solution -
/*
* @author-xyz
* @purpose-calculating time from simple pendulum's formula
*/
import java.util.*;
class SimplePendulum
{
public static void main()
{
float l,g;
double t;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the length - "); //entering l
l=sc.nextFloat();
System.out.println("Enter the acceleration due to gravity - "); //enter g
g=sc.nextFloat();
t=6.28*Math.sqrt(l/g); //calculating the time period
System.out.println("The time period is "+Math.round(t)+" seconds");
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. l float local Entering length
2. g float local Entering gravity
3. t double local Calculating time
Output –
Page | 9
Q6. Write a program by using class 'Employee' to accept Basic Pay of an
employee. Calculate the allowances/deductions as given below.
Solution -
/*
* @author-xyz
* @purpose-calculating the gross pay and net pay of an employee
*/
import java.util.*;
class Employee
{
public static void main()
{
int basic;
double da,hra,pf,gross,net;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your basic pay - ");
basic=sc.nextInt();
da=basic*30/100; //calculating dearness allowance
hra=basic*15/100; //calculating house rent allowance
pf=basic*12.5/100; //calculating provident fund
gross=basic+da+hra; //calculating gross pay
net=gross-pf;//calculating net pay
System.out.println("The gross pay of the employee is Rs."+gross);
System.out.println("The net pay of the employee is Rs."+net);
}
Page | 10
Variable Description
S.No Variable Data Type Scope Purpose
1. basic int local Entering salary
2. da double local Calculating Dearness Allowance
3. hra double local Calculating House Rent Allowance
4. pf double local Calculating Provident Fund
5. gross double local Calculating Gross Pay
6. net double local Calculating Net Pay
Output –
Page | 11
Q7. A shopkeeper offers a 10% discount on the printed price of a Digital Camera.
However, a customer must pay 6% GST on the remaining amount. Write a
program in Java to calculate the amount to be paid by the customer taking printed
price as an input.
Solution -
/*
* @author - xyz
* @purpose - calculating the discount on a digital Camera
*/
import java.util.*;
class Discount
{
public static void main()
{
double price,gst,disc,amt;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a price of the Digital Camera - ");
amt=sc.nextDouble();
disc=amt*10/100; //applying the discount
price=amt-disc; //calculating the price after the discount
gst=price*6/100; //adding gst of the discounted price
System.out.println("You have to pay Rs."+(price+gst));
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. price double local Calculating price
2. gst double local Getting GST on the amount
3. disc double local Calculating discount
4. amt double local Entering amount
Output -
Page | 12
Q8. Write a program to calculate the value of Pi with the help of the following series:
Pi = (4/1) - (4/3) + (4/5) - (4/7) + (4/9) - (4/11) + (4/13) - (4/15) .......
Hint: Use while loop with 100000 iterations.
Solution -
/*
* @author-xyz
* @purpose-calculating the value of pi using while loop
*/
class Pi
{
public static void main()
{
double pi=0;
int i=1,j=1;
while(i<=100000) //applying the while loop till 100000 iterations
{
if(i%2==0)
{
pi=pi-4/j; //subtracting for even numbers
}
else
{
pi=pi+4/j; //adding for odd numbers
}
j=j+2;
i++;
}
System.out.println("The value of PI is " + pi); //printing the value of pi
}
}
Page | 13
Variable Description
S.No Variable Data Type Scope Purpose
1. pi double local Calculating value of PI
2. i int local Looping purposes
3. j int local Looping purposes
Output -
Page | 14
Q9. Mr. Lokesh invests certain sum at 5% per annum compound interest for three
years. Write a program in Java to calculate:
(a) the interest for the first year
(b) the interest for the second year
(c) the amount after three years.
Take sum as an input from the user. Sample Input: Principal = ₹5000, Rate=10%,
Time = 3 yrs
Sample Output:
Interest for the first year: ₹500
Interest for the second year: ₹550
Interest for the third year: ₹605
Solution -
/*
* @author-xyz
* @purpose- calculating the compunded interest of an amount of principle
*/
import java.util.*;
class Interest
{
public static void main()
{
int prin,amt1,amt2,amt3,int1,int2,int3;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the principle - ");
prin=sc.nextInt();
int1=prin*5/100; //calculating interest for first year
amt1=prin+int1;
int2=amt1*5/100;
amt2=amt1+int2; //calculating amount for second year
int3=amt2*5/100;
Page | 15
amt3=amt2+int3; //calculating amount for third year
System.out.println("Interest for the first year is Rs."+int1);
System.out.println("Interest for the second year is Rs."+int2);
System.out.println("Interest for the third year is Rs."+int3);
System.out.println("The amount of third year is Rs."+amt3);
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. prin int local Entering principle
2. amt1 int local Calculating amount
3. amt2 int local Calculating amount
4. amt3 int local Calculating amount
5. int1 int local Calculating interest
6. int2 int local Calculating interest
7. int3 int local Calculating interest
Output –
Page | 16
Q10.A businessman wishes to accumulate 3000 shares of a company. However, he
already has some shares of that company valuing ₹10 (nominal value) which yield
10% dividend per annum and receive ₹2000 as dividend at the end of the year. Write a
program in Java to calculate the number of shares he has and how many more shares
to be purchased to make his target.
Hint: No. of share = (Annual dividend * 100) / (Nominal value * div%)
Solution -
/*
* @author-xyz
* @purpose-calculating no. of shares of a buisnessman
*/
import java.util.*;
class Shares
{
public static void main()
{
double shares;
Scanner sc=new Scanner(System.in);
shares=(2000*100)/(10*10); //calculating number of shares
System.out.println("Number of shares are "+shares);
System.out.println("The businessman needs "+(3000-shares)+" more shares to
complete his target.");
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. shares int local Calculating no. of shares
Output –
Page | 17
Q11.A certain amount is invested at the rate of 10% per annum for 3 years. Find the
difference between Compound Interest (CI) and Simple Interest (SI). Write a program
to take amount as an input.
Hint: SI = (P * R * T) / 100
A = P * (1 + (R/100))T
CI = A – P
Solution -
/*
* @author-xyz
* @purpose-finding the compound interest
*/
import java.util.*;
class CompundInterest
{
public static void main()
{
int prin,si;
double amt,ci,diff;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the principle - ");
prin=sc.nextInt();
si=(prin*10*3)/100; //calculating the simple interest
amt=(prin*(Math.pow((11/10),3))); //calculating the amount
ci=amt-prin; //calculating compound interest
diff=ci-si; //calculating difference between simple interest and compound interet
System.out.println("The difference between compund interest and simple interest
is Rs."+diff);
}
}
Page | 18
Variable Description
S.No Variable Data Type Scope Purpose
1. prin int local Entering principle
2. si int local Calculating simple interest
3. ci double local Calculating compound
interest
4. amt double local Calculating amount
5. diff double local Calculating difference
Output -
Page | 19
Q12.A shopkeeper sells two calculators for the same price. He earns 20% profit on
one and suffers a loss of 20% on the other. Write a program to find his total cost price
of the calculators by taking selling price as input.
Hint: CP = (SP / (1 + (profit / 100))) (when profit)
CP = (SP / (1 - (loss / 100))) (when loss)
Solution -
/*
* @author-xyz
* @purpose-calculating the cost price of calculators when in profit and in loss
*/
import java.util.*;
class Shopkeeper
{
public static void main()
{
int sp;
double cpgain,cploss;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the selling price - ");
sp=sc.nextInt();
cpgain=(sp/(1+0.2)); //calculating the gain the price of calculator
cploss=(sp/(1-0.2)); //calculating the loss the price of calculator
System.out.println("The total cost price of the calculators are
Rs."+(cpgain+cploss));
}
}
Page | 20
Variable Description
S.No Variable Data Type Scope Purpose
1. sp int local Entering selling price
2. cpgain double local Calculating gain
3. cploss double local Calculating loss
Output –
Page | 21
Q13.Write a program to input a number and evaluate the results based on the number
entered by the user:
(a) Natural logarithm of the number
(b) Absolute value of the number
(c) Square root of the number
(d) Cube of the number
(e) Random numbers between 0 (zero) and 1 (one).
Solution -
/*
* @author-xyz
* @purpose-applying math functions of a number
*/
import java.util.*;
class Mathfunctions
{
public static void main()
{
int num;
Scanner sc=new Scanner(System.in);
System.out.println("Enter a number - ");
num=sc.nextInt();
System.out.println("The natural logarithm of the number is " +
(Math.log(num))); //calculating the log of the number
System.out.println("The absolute value of the number is " +
(Math.abs(num))); //calculating the absolute value of the number
System.out.println("The square root of the number is "+(Math.sqrt(num)));
System.out.println("The cube of the number is "+(Math.cbrt(num)));
System.out.println("A random numbers between 0 and 1 is
"+(Math.abs(num)));
}
Page | 22
}
Variable Description
S.No Variable Data Type Scope Purpose
1. num int local Entering a number
Output –
Page | 23
Q14.You want to calculate the radius of a circle by using the formula:
Area = (22/7) * r2; where r = radius of a circle
Hence the radius can be calculated as: r = √((7 * area) / 22)
Write a program in Java to calculate and display the radius of a circle by taking area as
an input.
Solution –
/*
* @author - xyz
* @purpose - calculating radius with the area formula
*/
import java.util.*;
class Radius
{
public static void main()
{
int area;
double r;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the area (in cm) - "); //entering area
area=sc.nextInt();
r=Math.sqrt((7*area)/22); //calculating radius
System.out.println("The radius is "+r+"cm");
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. area Int Local Entering area
2. r Double local Calculating radius
Page | 24
Output -
Page | 25
Q15.Write a program to input three numbers and check whether they are equal or not.
If they are unequal numbers then display the greatest among them otherwise, display
the message 'All the numbers are equal'.
Sample Input: 34, 87, 61
Sample Output: Greatest number: 87
Sample Input: 81, 81, 81
Sample Output: All the numbers are equal.
Solution -
/*
* @author - xyz
* @purpose – finding the greatest number among 3
*/
import java.util.*;
class Numbers
{
public static void main()
{
int num1,num2,num3;
Scanner sc=new Scanner(System.in);
System.out.println("Enter three numbers - ");
num1=sc.nextInt();
num2=sc.nextInt();
num3=sc.nextInt();
if((num1!=num2) && (num2!=num3)&&(num1!=num2))
{
if(num1>=num2 && num1>=num3) //if number is greater than other two
{
System.out.println(num1 + " is the largest number");
}
Page | 26
else if (num2>=num1 && num2>=num3) //if number is greater than other two
{
System.out.println(num2 + " is the largest number");
}
else
{
System.out.println(num3 + " is the largest number");
}
}
else
{
System.out.println("All the numbers are equal");
}
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. num1 int local Entering number
2. num2 int local Entering number
3. num3 int local Entering number
Output -
Page | 27
Q16.Using a switch statement, write a menu driven program to:
(a) Generate and display the first 10 terms of the Fibonacci series 0, 1, 1, 2, 3, 5 The
first two Fibonacci numbers are 0 and 1, and each subsequent number is the sum of the
previous two.
(b) Find the sum of the digits of an integer that is input. Sample Input: 15390 Sample
Output: Sum of the digits = 18 For an incorrect choice, an appropriate error message
should be displayed.
Solution -
/*
* @author - xyz
* @purpose - switch case to print Fibonacci series or sum of the digits of a numb
*/
import java.util.*;
class Switch
{
public static void main()
{
int sw;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 to print Fibonacci Series and 2 to print the sum
of the digits - ");
sw=sc.nextInt();
switch(sw)
{
case 1: //printing fibonacci series
{
int a=0,b=1,i,c;
System.out.print(a+", "+b);
for (i=3;i<=10;i++)
{
c=a+b;
Page | 28
System.out.print(", " + c);
a=b;
b=c;
} break;
}
case 2: //printing sum of the digits of a number
{
int num,sum=0;
System.out.print("Enter a number - ");
num = sc.nextInt();
while (num!=0)
{
sum=sum+(num%10); //calculating sum
num=num/10;
}
System.out.println("The sum of the digits is " + sum);
break;
}
default:
{
System.out.println("Incorrect choice");
break;
}
}
}
}
Page | 29
Variable Description
S.No Variable Data Type Scope Purpose
1. sw int local Switch case purpose
2. a int local Fibonacci series use
3. b int local Fibonacci series use
4. c int local Fibonacci series use
5. i int local Looping purposes
6. num int local Entering number
7. sum int local Calculating sum of the digits
Output -
Page | 30
Q.17 Print the following pattern –
*
**
***
****
*****
****
***
**
*
Solution –
/*
* @author - xyz
* @purpose – printing a diamond pattern
*/
import java.util.*;
class Diamond
{
public static void main()
{
int i,j;
for(i=1;i<=5;i++) //to insert space in the upper diamond
{
System.out.print(" ");
for(j=1;j<=5-i;j++) //to insert space
{
System.out.print(" ");
}
for(j=1;j<=i;j++) //insert the star pattern
{
System.out.print("* ");
Page | 31
}
System.out.println();
}
for(i=4;i>=1;i--) //insert space in lower part of the diamond
{
System.out.print(" ");
for(j=1;j<=5-i;j++)
{
System.out.print(" ");
}
for(j=1;j<=i;j++)
{
System.out.print("* ");
}
System.out.println();
}
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. i int local Looping purpose
2. j int local Looping purpose
Output -
Page | 32
Q18.An Electricity Board charges for electricity per month from their consumers
according to the units consumed. The tariff is given below:
Units Consumed Charges
Up to 200 units ₹3.80/unit
More than 200 units and up to 300 units ₹4.40/unit
More than 300 units and up to 400 units ₹5.10/unit
More than 400 ₹5.80/unit
Solution -
/*
* @author - xyz
* @purpose - calculating the electricity bill
*/
import java.util.*;
class ElectricityBill
{
public static void main()
{
int units;
String name;
Scanner sc=new Scanner(System.in);
System.out.println("Enter your name - ");
name=sc.nextLine();
System.out.println("Enter the number of units consumed - ");
units=sc.nextInt();
if(units<=200) //calculating the price is the unit is less than 200
{
System.out.println(name+" you have to pay Rs."+units*3.8);
}
else if(units>200 && units<=300) //calculate price when unit is less than 200
{
Page | 33
System.out.println(name+" you have to pay Rs."+units*4.4);
}
else if(units>300 && units<=400)
{
System.out.println(name+" you have to pay Rs."+units*5.1);
}
else
{
System.out.println(name+" you have to pay Rs."+units*5.8);
}
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. units int local Entering no. of units
2. name string loca Entering name of user
Output -
Page | 34
Q19.Write a program to generate a triangle or an inverted triangle based upon
user's choice of triangle to be displayed.
Example 1:
Input: Type 1 for a triangle
Enter your choice: 1
Sample Output:
1
22
333
4444
55555
Example 2:
Input: Type 2 for an inverted triangle
Enter your choice: 2
Sample Output:
55555
4444
333
22
1
Solution -
/*
* @author-xyz
* @purpose-create two pattern and printing as per user’s choice
*/
import java.util.*;
class Pattern
{
public static void Pattern()
{
Page | 35
int sw,i,j;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 1 for a triangle and 2 for an inverted triangle - ");
sw=sc.nextInt();
switch(sw)
{
case 1: //printing triangle
{
for (i=1;i<=5;i++)
{
for (j=1;j<=i;j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
}
case 2: //printing inverted triangle
{
for (i=5;i>0;i--)
{
for (j=1;j<=i;j++)
{
System.out.print(i + " ");
}
System.out.println();
}
break;
}
default:
Page | 36
{
System.out.println("Enter either 1 or 2");
break;
}
}
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. sw int local Switch case purpose
2. i int local Looping purposes
3. j int local Looping purposes
Output –
Page | 37
Q20.Write a program to input a number and perform the following
tasks:
(a) to check whether it is a prime number or not
(b) to reverse the number
If the number as well as the reverse is also 'Prime' then display 'Twisted
Prime' otherwise 'Not a twisted Prime'.
Sample Input: 167
Sample Output: 167 and 761 both are prime.
It is a 'Twisted Prime'.
Solution -
/*
* @author-xyz
* @purpose-check if the number is twisted prime or not
*/
import java.util.*;
class TwistedPrime
{
public static void main()
{
int num,c=0,h,d=0,j=0,i;
Scanner sc=new Scanner(System.in);
System.out.print("Enter a number - ");
num=sc.nextInt();
for(i=1;i<=num;i++) //checking if number is prime
{
if(num%i==0)
{
c=c+1;
}
}
while(num!=0)
{
Page | 38
h=num%10;
d=d*10+h; //reversing the number
num=num/10;
}
for(i=1;i<=d;i++) //checking if reversed number is prime or not
{
if(d%i==0)
{
j=j+1;
}
}
if ((c==2) &&(j==2))
{
System.out.println("It is twisted prime number ");
}
else
{
System.out.println("It is not a twisted prime number ");
}
}
}
Variable Description
S.No Variable Data Type Scope Purpose
1. num int local Entering number
2. c int local Checking if number is prime
3. h int local Reversing number
4. d int local Reversing number
5. j int local Checking if number is prime
6. i int local Looping purposes
Output –
Page | 39
Conclusion –
Writing codes for Java programs was a great learning experience. It helped me
understand how to use the concepts I learned in class to solve problems. Each
program I wrote taught me how to think logically and find solutions.
Even though some parts were challenging, solving them made me feel proud and
confident. Using BlueJ made coding easier and more fun. Overall, I really
enjoyed working on Java programs, and it has made me more interested in
programming.
Page | 40