IX Record Programs 24 - 25
IX Record Programs 24 - 25
Question 1
Mr. Agarwal invests certain sum at 10% per annum compound interest for three years. Write a program
in Java to calculate and display:
(a) The interest for the first year
(b) The interest for the second year
(c) The interest for the third year
Sample Input: Principal = Rs.5000
Sample Output:
Interest for the First year: Rs.500
Interest for the Second year: Rs.550
Interest for the Third year: Rs.605
Solution:
import java.util.Scanner;
public class CompInterest
{
public static void main(String args[])
{
double amount,first=0.0,second=0.0,third=0.0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the Principle amount:");
amount=sc.nextDouble();
first=amount*10/100;
amount+=first;
second=amount*10/100;
amount+=second;
third=amount*10/100;
System.out.println("Interest for the First year: Rs."+first);
System.out.println("Interest for the Second year: Rs."+second);
System.out.println("Interest for the Third year: Rs."+third);
}
}
Variable Description:
Variable Data Type Description
amount double To store the Principle amount
first double To store the first year’s interest
second double To store the second year’s interest
third double To store the third year’s interest
Output:
Question 2
Write a program to input the time in seconds. Display the time after converting them into hours, minutes
and seconds.
Sample Input: Time in seconds: 5420
Sample Output: 1 Hour 30 Minutes 20 Seconds
Solution:
import java.util.Scanner;
public class TimeCalc
{
public static void main(String args[])
{
int time,hr=0,min=0,sec=0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the time in Seconds:");
time=sc.nextInt();
hr=time/3600;
time=time%3600;
min=time/60;
sec=time%60;
System.out.println(hr+" Hour "+min+" Minutes "+sec+" Seconds");
}
}
Variable Description:
Variable Data Type Description
time int To store the time in seconds
hr int To store the hour
min int To store the minutes
sec int To store the seconds
Output:
Question 3
Write a program by using class ‘Employee’ to accept Basic Pay of an employee. Calculate the
allowances/deductions as given below and finally, find and display the Gross and Net pay.
Allowance/Deduction Rate:
Dearness Allowance (DA) : 30% of Basic Pay
House Rent Allowance (HRA) : 15% of Basic Pay
Provident Fund (PF) : 12.5% of Basic Pay
Gross Pay = Basic Pay + Dearness Allowance + House Rent Allowance
Net Pay = Gross Pay – Provident Fund
Solution:
import java.util.Scanner;
public class SalaryCalc
{
public static void main(String args[])
{
double Basic,DA=0.0,HRA=0.0,PF=0.0,Gross=0.0,Netpay=0.0;
Scanner sc= new Scanner(System.in);
System.out.print("Enter the Basic Pay:");
Basic=sc.nextDouble();
DA=Basic*30/100;
HRA=Basic*15/100;
PF=Basic*12.5/100;
Gross=Basic+DA+HRA;
Netpay=Gross-PF;
System.out.println("Basic Pay:"+Basic);
System.out.println("Dearness Allowance:"+DA);
System.out.println("House Rent Allowance:"+HRA);
System.out.println("Provident fund:"+PF);
System.out.println("Gross Pay:"+Gross);
System.out.println("Net Pay:"+Netpay);
}
}
Variable Description:
Variable Data Type Description
Basic double To store the Basic pay
DA double To store the Dearness allowance
HRA double To store the House Rent allowance
PF double To store the Provident fund
Gross double To store the Gross pay
Netpay double To store the Net pay
Output:
Question 4
Write a program to input three angles of a triangle and check whether its construction is possible or not.
If possible then check and display whether it is an acute-angled triangle, right-angled or an obtuse-
angled triangle. Otherwise display ‘A Triangle is not possible’.
import java.util.Scanner;
public class TypeAngle
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a1,a2,a3;
System.out.println("Enter three angles:");
a1=sc.nextInt();
a2=sc.nextInt();
a3=sc.nextInt();
if((a1+a2+a3)==180)
{
if(a1==90||a2==90||a3==90)
System.out.println("Right-Angled Triangle");
else if(a1>90||a2>90||a3>90)
System.out.println("Obtuse-Angled Triangle");
else
System.out.println("Acute-Angled Triangle");
}
else
System.out.println("Triangle cannot be constructed");
}
}
Variable Description:
Variable Data Type Description
a1, a2, a3 int To store the angles of triangle
Output:
Question 5
Write a program to input the cost price and the selling price of an article. If the selling price is more than
the cost price then calculate and display the actual profit and profit percent otherwise calculate and
display the actual loss and loss percent. If the cost price and the selling price are equal, the program
displays the message ‘Neither profit nor loss’.
Solution:
import java.util.Scanner;
public class ProfitLoss
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int cp,sp,profit,loss;
double ppercent,lpercent;
System.out.print("Enter the Cost Price of the article: ");
cp=sc.nextInt();
System.out.print("Enter the Selling Price of the article: ");
sp=sc.nextInt();
if(sp==cp)
{
System.out.println("Neither profit or loss");
}
else if(sp>cp)
{
profit=sp-cp;
ppercent=(profit*100)/cp;
System.out.println("Profit: "+profit);
System.out.println("Profit Percentage: "+ppercent);
}
else if(sp<cp)
{
loss=cp-sp;
lpercent=(loss*100)/cp;
System.out.println("Loss: "+loss);
System.out.println("Loss Percentage: "+lpercent);
}
}
}
Variable Description:
Variable Data Type Description
cp int To store the cost price of the article
sp int To store the selling price of the article
profit int To store the profit amount
loss int To store the loss amount
ppercent double To store the profit percentage
lpercent double To store the loss percentage
Output:
Question 6
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:
import java.util.Scanner;
public class Greatest
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int num1,num2,num3;
System.out.println("Enter three numbers:");
num1=sc.nextInt();
num2=sc.nextInt();
num3=sc.nextInt();
if(num1==num2 && num2==num3)
System.out.println("All the numbers are equal");
else if(num1>num2 && num1>num3)
System.out.println("Greatest number: "+num1);
else if(num2>num3)
System.out.println("Greatest number: "+num2);
else
System.out.println("Greatest number: "+num3);
}
}
Variable Description:
Variable Data Type Description
num1, num2, int To store the numbers
num3
Output:
Question 7
Write a program to input the year. The program checks and displays whether it is:
(a) Leap year
(b) A Century Leap year
(c) A Century year but not a Leap year
Solution:
import java.util.Scanner;
public class LeapYear
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int year;
System.out.println("Enter the year:");
year=sc.nextInt();
if(year%400==0)
System.out.println("A Century Leap Year");
else if(year%100==0)
System.out.println("A Century Year but not a Leap Year");
else if(year%4==0)
System.out.println("A Leap Year");
else
System.out.println("Not a Leap Year");
}
}
Variable Description:
Variable Data Type Description
year int To store the year
Output:
Question 8
A cloth showroom has announced festival discounts and the gifts on the purchase of items, based on the
total cost as given below:
Total Cost Discount Gift
Up to Rs.2,000 5% Calculator
Rs.2,001 to Rs.5,000 10% School Bag
Rs.5001 to Rs.10,000 15% Wall Clock
Above Rs.10,000 20% Wrist Watch
Write a program to input the total cost. Compute and display the amount to be paid by the customer
along with the gift.
Solution:
import java.util.Scanner;
public class ClothShowroom
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
double cost,discount=0.0,amount=0.0;
String gift="";
System.out.print("Enter the Total Cost: ");
cost=sc.nextDouble();
if(cost<=2000)
{
discount=cost*5/100;
gift="Calculator";
}
else if(cost>=2001 && cost<=5000)
{
discount=cost*10/100;
gift="School Bag";
}
else if(cost>=5001 && cost<=10000)
{
discount=cost*15/100;
gift="Wall Clock";
}
else if(cost>10000)
{
discount=cost*20/100;
gift="Wrist Watch";
}
amount=cost-discount;
System.out.println("Discount: "+discount);
System.out.println("Gift: "+gift);
System.out.println("Amount to be paid: "+amount);
}
}
Variable Description:
Variable Data Type Description
cost double To store the Total Cost
discount double To store the discount amount
amount double To store the amount to be paid
gift String To store the gift
Output:
Question 9
A Pre-Paid taxi charges from the passenger as per the tariff given below:
Distance Rate
Up to 5 km Rs.100
For the next 10 km Rs.10/km
For the next 10 km Rs.8/km
More than 25 km Rs.5/km
Write a program to input the distance covered and calculate the amount to be paid by the passenger.
The program displays the printed bill with the details given below:
Taxi No. : ___________________
Distance Covered : ___________________
Amount : ___________________
Solution:
import java.util.Scanner;
public class PrePaidTaxi
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
String taxino;
int distance,amount=0;
System.out.print("Enter the Taxi No: ");
taxino=sc.next();
System.out.print("Enter the Distance: ");
distance=sc.nextInt();
if(distance<=5)
amount=100;
else if(distance>5 && distance<=15)
amount=(distance-5)*10+100;
else if(distance>15 && distance<=25)
amount=(distance-15)*8+200;
else if(distance>25)
amount=(distance-25)*5+280;
System.out.println("Taxi No.: "+taxino);
System.out.println("Distance Covered: "+distance);
System.out.println("Amount: "+amount);
}
}
Variable Description:
Variable Data Type Description
taxino String To store the Taxi number
distance int To store the distance travelled
amount int To store the amount to be paid
Output:
Question 10
Write a program using switch case to find the volume of a cube, a sphere and a cuboid. For an incorrect
choice, an appropriate error message should be displayed.
a) Volume of cube = s * s * s
b) Volume of sphere = π* r * r * r ( π = )
c) Volume of cuboid = l * b * h
Solution:
import java.util.Scanner;
public class MenuDriven
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int s,r,l,b,h,choice;
double pi=3.14,volume=0.0;
System.out.println("Menu");
System.out.println("1.Volume of cube");
System.out.println("2.Volume of sphere");
System.out.println("3.Volume of cuboid");
System.out.print("Enter your choice: ");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.print("Enter the side value: ");
s=sc.nextInt();
volume=Math.pow(s,3);
System.out.print("The volume of cube: "+volume);
break;
case 2:
System.out.print("Enter the radius value: ");
r=sc.nextInt();
volume=(4/3)*(pi*Math.pow(r,3));
System.out.print("The volume of cube: "+volume);
break;
case 3:
System.out.print("Enter the value of l,b and h: ");
l=sc.nextInt();
b=sc.nextInt();
h=sc.nextInt();
volume=l*b*h;
System.out.print("The volume of cube: "+volume);
break;
default:
System.out.println("Wrong choice");
}
}
}
Variable Description:
Variable Data Type Description
s int To store the side value
r int To store the radius value
l int To store the length value
b int To store the breadth value
h int To store the height calue
choice int To store the user’s choice
pi double To store the pi value
volume double To store the volume of shape
Output:
Question 11
Write a program in Java to display the Fibonacci series for ‘n’ terms
0, 1, 1, 2, 3, 5, 8 …… n terms
Solution:
import java.util.Scanner;
public class Fibonacci
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a=-1,b=1,c=0;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt();
System.out.println("Fibonacci series for "+n+" terms:");
for(int i=1;i<=n;i++)
{
c=a+b;
System.out.print(c+" ");
a=b;
b=c;
}
}
}
Variable Description:
Variable Data Type Description
a, b, c int To store the terms
n int To store the number of terms
i int Loop variable
Output:
Question 12
Write a program in Java to display the Tribonacci series for ‘n’ terms
0, 1, 2, 3, 6, 11, 20, 37 …… n terms
Solution:
import java.util.Scanner;
public class Tribonacci
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
int a=-1,b=0,c=1,d=0;
System.out.print("Enter the number of terms: ");
int n=sc.nextInt();
System.out.println("Tribonacci series for "+n+" terms:");
for(int i=1;i<=n;i++)
{
d=a+b+c;
System.out.print(d+" ");
a=b;
b=c;
c=d;
}
}
}
Variable Description:
Variable Data Type Description
a, b, c, d int To store the terms
n int To store the number of terms
i int Loop variable
Output:
Question 13
Write the program in Java to display the first ten terms of the following series.
a) 1, 9, 25, 49, …….
b) 2, 5, 10, 17, ......
Solution:
Variable Description:
Variable Data Type Description
i, j int Loop variables
term int To store the terms
Output: a)
Output: b)
Question 14
Write the program in Java to find and display the sum of the following series.
a) (1*2) + (2*3)+ (3*4)+ …….+ (19*20)
b) + + + …….. to n terms
Solution:
b) + + + …….. to n terms
import java.util.Scanner;
public class Series4
{
public static void main(String[] args)
{
int i,j,n;
double a,sum=0.0;
Scanner sc=new Scanner(System.in);
System.out.print("Enter number of terms:");
n=sc.nextInt();
System.out.print("Enter the value of a:");
a=sc.nextDouble();
for(i=1,j=1;i<=n;i++,j+=2)
{
sum=sum+(Math.pow(j,2)/Math.pow(a,i));
}
System.out.println("The sum of series:"+sum);
}
}
Variable Description:
Variable Data Type Description
i, j int Loop variables
n int To store the number of terms
a double To store the value of ‘a’
sum int/double To store the sum of terms
Output: a)
Output: b)
Question 15
Write a program in Java to enter a number and check whether the number is an Armstrong number or
not.
(A number is said to be Armstrong if the sum of the cubes of its digits is equal to the original number
Example: 13 + 53 + 33 = 153).
Sample Input: 153
Sample Output: 153 is an Armstrong Number
Solution:
import java.util.Scanner;
public class Armstrong
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int num,digit=0,arm=0,num1=0;
System.out.print("Enter a number:");
num=in.nextInt();
num1=num;
while(num!=0)
{
digit=num%10;
arm=arm+(int)Math.pow(digit,3);
num=num/10;
}
if(num1==arm)
System.out.println(num1+" is an Armstrong number");
else
System.out.println(num1+" is not an Armstrong number");
}
}
Variable Description:
Variable Data Type Description
num int To store the number
num1 int To store the number into a temporary variable
digit int To store the digits
arm int To store the Armstrong number
Output:
Question 16
Write a program in Java to enter a number and check whether the number is a Palindrome number or
not.
(A number is a Palindrome which when read in the reverse order is the same as the original number).
Sample Input: 131
Sample Output: 131 is Palindrome Number
Sample Input: 113
Sample Output: 113 is not a Palindrome Number
Solution:
import java.util.Scanner;
public class PalinNumber
{
public static void main(String[] args)
{
Scanner in=new Scanner(System.in);
int num,digit=0,rev=0,num1=0;
System.out.print("Enter a number:");
num=in.nextInt();
num1=num;
while(num!=0)
{
digit=num%10;
rev=rev*10+digit;
num=num/10;
}
if(num1==rev)
System.out.println(num1+" is Palindrome Number");
else
System.out.println(num1+" is not Palindrome Number");
}
}
Variable Description:
Variable Data Type Description
num int To store the number
num1 int To store the number into a temporary variable
digit int To store the digits
rev int To store the reverse number
Output:
Question 17
Write a program to input a number. Calculate and display the factorial of each digit.
Sample Input: 365
Factorial of 5 = 120
Factorial of 6 = 720
Factorial of 3 = 6
Solution:
import java.util.Scanner;
public class DigitFactorial
{
public static void main(String args[])
{
int num,i,digit=0,fact;
Scanner sc=new Scanner(System.in);
System.out.print("Enter the number:");
num=sc.nextInt();
while(num!=0)
{
fact=1;
digit=num%10;
num=num/10;
for(i=1;i<=digit;i++)
{
fact=fact*i;
}
System.out.println("Factorial of "+digit+" = "+fact);
}
}
}
Variable Description:
Variable Data Type Description
num int To store the number
i int Loop variable
digit int To store the digits
fact int To store the factorial
Output:
Question 18
Write a program to display all composite numbers from 1 to 100. A number is said to be composite, if it
has two or more factors excluding 1 and the number itself.
Example: Input number 6.
Factors of 6 are 2 and 3.
Hence, 6 is a composite number.
Few composite numbers are 4, 6, 8 ,9, 10, 12, 14, 15, 16, 18, 20, 21, ……..
Solution:
Variable Description:
Variable Data Type Description
i, j int Loop variable
count int To store the count values
Output:
Question 19
Write the programs in Java to display the following patterns:
a) Pattern1 b) Pattern2
1 1 2 3 4 5
1 2 1 2 3 4
1 2 3 1 2 3
1 2 3 4 1 2
1 2 3 4 5 1
Solution:
a)
public class Pattern1
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=i;j>=1;j--)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
b)
public class Pattern2
{
public static void main(String args[])
{
for(int i=5;i>=1;i--)
{
for(int j=1;j<=i;j++)
{
System.out.print(j+"\t");
}
System.out.println();
}
}
}
Variable Description:
Variable Data Type Description
i, j int Loop variables
Output: Pattern1
Output: Pattern2
Question 20
Write the programs in Java to display the following patterns:
a) b)
* 1
* # 2 3
* # * 4 5 6
* # * # 7 8 9 10
* # * # * 11 12 13 14 15
Solution:
a)
public class Pattern3
{
public static void main(String args[])
{
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
if(j%2==0)
System.out.print("# \t");
else
System.out.print("* \t");
}
System.out.println();
}
}
}
b)
public class Pattern4
{
public static void main(String args[])
{
int k=1;
for(int i=1;i<=5;i++)
{
for(int j=1;j<=i;j++)
{
System.out.print(k+"\t");
k++;
}
System.out.println();
}
}
}
Variable Description:
Variable Data Type Description
i, j int Loop variables
k int To store continuous values
Output: a)
Output: b)