APC Solved Chapter 5

1.
Mr. Shyam Lal Agarwal invests certain sum at 5% per annum compound interest for three years. Write a program in Java to calculate:

the interest for the first year
the interest for the second year
the amount after three years.
Take sum as an input from the user.

import java.io.*;

public class Q1

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int p; double SI1,SI2,SI3,A3;

System.out.println(“Enter the principal”);

p=Integer.parseInt(in.readLine());

SI1=(p*5.0*1)/100.0;

System.out.println(“Interest for the first year =”+SI1);

SI2=((p+SI1)*5.0*1)/100.0;

System.out.println(“Interest for the second year =”+SI2);

SI3=((SI1+SI2+p)*5.0*1)/100;

A3=SI1+SI2+SI3+p;

System.out.println(“Amount after three years =”+A3);

}

}

Sample Input:       10000

Sample Output:    Interest for the first year =500.0

Interest for the second year =525.0

Amount after three years =11576.25

2.
A shopkeeper offers 10% discount on the printed price of a Digital Camera. However, a customer has to pay 6% sales tax 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.

import java.io.*;

public class Q2

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

double a;

double dis,ra,st,pa;

System.out.println(“Enter the marked price of the camera”);

a=Double.parseDouble(in.readLine());

dis=30.0/100.0*a;

ra=a-dis;

st=6.0/100.0*a;

pa=ra+st;

System.out.println(“paid amount=”+pa);

}

}

Sample Input:      25000

Sample Output:    paid amount=19000.0

3.
A man invests certain sum of money at 5% compound interest for the first year, 8% for the second year and 10% for the third year. Write a program in Java to calculate the amount after three years, taking the sum as an input.

[Hint: A = P(1 + R1/100)(1 +R2/100)(1 +R3/100)]

import java.io.*;

public class Q3

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

double p,A;

System.out.println(“Enter the amount of sum”);

p=Double.parseDouble(in.readLine());

A=p*(1.0+5.0/100.0)*(1.0+8.0/100.0)*(1.0+10.0/100.0);

System.out.println(“Amount after three years = ” +A);

}

}

Sample Input:     15000

Sample Output:  Amount after three years = 18711.0

4.
A shopkeeper offers 30% discount on purchasing articles whereas the other shopkeeper offers two successive discount 20% and 10% for purchasing the same articles. Write a program in Java to compute and display which is better offer for a customer. Take the price of an article as an input.

import java.io.*;

public class Q4

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

double p,dis1,dis2,sDis1,sDis2,RA;

System.out.println(“Enter the price”);

p=Double.parseDouble(in.readLine());

dis1=30.0/100.0*p;

sDis1=20.0/100.0*p;

RA=p-sDis1;

sDis2=10.0/100.0*RA;

dis2=(sDis1+sDis2);

if (sDis1>sDis2)

System.out.println(“First scheme of single discount is better”);

else

System.out.println(“Second scheme of successive discount is better”);

}

}

Sample Input:      16500

Sample Output:   First scheme of single discount is better

5.
Mr. Guha wishes to accumulate 3000 shares of a company. However, already he has some shares of that company valuing 10 (nominal value) which yield 10% dividend per annum and received 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.

public class Q5

{

public static void main(String args[])

{

double annDivi,nv,divperc,totshare,NoS,StoPur;

annDivi=2000.0;

nv=10.0;

divperc=10.0;

totshare=3000.0;

NoS=(annDivi*100.0)/(nv*divperc);

System.out.println(“number of shares he have = “+NoS);

StoPur=totshare-NoS;

System.out.println(“No. of shares to be purchased = “+StoPur);

}

}

Output:   number of shares he have = 2000.0

No. of shares to be purchased to satisfy his wish = 1000.0

6.
Write a program in Java to calculate the value of sin30° (degree), cos 30° (degree) and tan30° (degree). Now, display the lowest value of the trigonometrical ratios.

public class Q6

{

public static void main(String args[])

{

double a,r,s,c,t;

a=30;

r=22.0/(7.0*180.0)*a;

s=Math.sin(r);

c=Math.cos(r);

t=Math.tan(r);

System.out.println(“sin 30 =”+s);

System.out.println(“cos 30 =”+c);

System.out.println(“tan 30 =”+t);

if(s<c && s<t)

System.out.println(“sin 30 is the smallest”);

else if(c<t)

System.out.println(“cos 30 is the smallest”);

else

System.out.println(“tan 30 is the smallest”);

}

}

Output:     sin 30 =0.5001825021996698

cos 30 =0.86592001044743

tan 30 =0.5776313010034497

sin 30 is the smallest

7.
Write a program in Java to accept the cost price and the selling price of an article and calculate either profit percent or loss percent.

import java.io.*;

public class Q7

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

double cp,sp,p,l,pper,lper;

System.out.println(“Enter the cost price”);

cp=Double.parseDouble(in.readLine());

System.out.println(“Enter the selling price”);

sp=Double.parseDouble(in.readLine());

if(sp>cp)

{

p=sp-cp;

pper=p/cp*100.0;

System.out.println(“Profit percent = “+ pper);

}

else

{

l=cp-sp;

lper=l/cp*100.0;

System.out.println(“loss percent =”+lper);

}

}

}

Sample Input:    CP = 8000

SP  = 9541

Sample Output: Profit percent = 19.2625

8.
Write a program to input three sides of a triangle and check whether the triangle is possible or not. If possible then display whether it is an Equilateral, Iso-sceles or a Scalene Triangle otherwise, display ‘Triangle not possible’.

import java.io.*;

public class Q8

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int a,b,c;

System.out.println(“Enter the three sides of triangle”);

a=Integer.parseInt(in.readLine());

b=Integer.parseInt(in.readLine());

c=Integer.parseInt(in.readLine());

if((a+b>c)&&(b+c>a)&&(a+c>b))

{

System.out.println(“Triangle is possible”);

if((a==b)&&(b==c)&&(c==a))

System.out.println(“Equilateral triangle”);

else if((a==b)||(b==c)||(c==a))

System.out.println(“isoceles triangle”);

else if((a!=b)&&(b!=c)&&(c!=a))

System.out.println(“scalene triangle”);

}

else

System.out.println(“Triangle is not possible”);

}

}

Sample Input:    12, 13, 14

Sample Output:  Triangle is possible

scalene triangle

9.
Atul Transport’ charges for carrying parcels as per the city and the weight. The tariff is given below:

City                          Weight                                Charges

Kolkata                   Up to 100 kg                       45/kg

More than 100 kg              75/kg

Mumbai                  Up to 100 kg                       65/kg

More than 100 kg              95/kg

Chennai                  Up to 100 kg                       75/kg

More than 100 kg              115/kg

Delhi                        Up to 100 kg                        90/kg

More than 100 kg               125/kg

Write a program to input city name ‘K’ for Kolkata, ‘M’ for Mumbai, ‘C’ for Chennai, `D’ for Delhi and weight of the parcel. Calculate and display the charges to be paid by a customer for booking.

import java.io.*;

public class Q9

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int w,amount;char ch;

System.out.println(“Enter the weight of the parcel”);

w=Integer.parseInt(in.readLine());

System.out.println(“Enter the city name ‘k’ for kolkata ‘m’for mumbai ‘c’for chennai and ‘d’for delhi”);

ch=(char)in.read();

if(ch==’k’)

{

if(w<=100)

{

amount=w*45;

System.out.println(“charges to be paid =”+amount);

}

else

{

amount=(w-100)*75+100*45;

System.out.println(“charges to be paid =”+amount);

}

}

else if(ch==’m’)

{

if(w<=100)

{

amount=w*65;

System.out.println(“charges to be paid =”+amount);

}

else

{

amount=(w-100)*95+100*65;

System.out.println(“charges to be paid =”+amount);

}

}

else if(ch==’c’)

{

if(w<=100)

{

amount=w*75;

System.out.println(“charges to be paid =”+amount);

}

else

{

amount=(w-100)*115+100*75;

System.out.println(“charges to be paid =”+amount);

}

}

else if(ch==’d’)

{

if(w<=100)

{

amount=w*90;

System.out.println(“charges to be paid =”+amount);

}

else

{

amount=(w-100)*125+100*90;

System.out.println(“charges to be paid =”+amount);

}

}

}

}

Sample Input:   450, k

Sample Output: charges to be paid =30750

10.
Write a program in Java to compute charges for sending parcels when the charges are as follows:

For the first 1 kg: Z 15.00

For additional weight, for every 500 gm or fraction thereof: 8.00

import java.io.*;

public class Q10

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int w,c,x,a;

System.out.println(“Enter the weight in grams”);

w=Integer.parseInt(in.readLine());

if(w<=1000)

System.out.println(“Charge = Rs. 15.00”);

else

{

x=(w-1000)/500;

a=w%500;

{

if(a==0)

{

c=x*8+15;

System.out.println(“charge=”+c);

}

else

{

c=x*8+15+8;

System.out.println(“charge=”+c);

}

}

}

}

}

Sample Input:      1500

Sample Output:  charge=23

11.
To foster a sense of water conservation, the water department has an annual water conservation tax policy. The rates are based on the water consumption of the consumer. The tax rates are as follows:

Water consumed (in Gallons)      Tax Rate in ‘/100 gallons

Up to 45                                                          No Tax

More than 45 but 75 or less                           475.00

More than 75 but 125 or less                         750.00

More than 125 but 200 or less                     1225.00

More than 200 but 350 or less                     1650.00

More than 350                                              2000.00

Write a program in Java to compute and display an annual water tax chart based on

the input value of water consumption.

import java.io.*;

public class Q11

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int q;double c;

System.out.println(“Enter the quantity of water consumed in gallons”);

q=Integer.parseInt(in.readLine());

if(q<=45)

System.out.println(“NO TAX”);

else if(q>45 && q<=75)

{

c=475.0/100.0*q;

System.out.println(“TAX = “+c);

}

else if(q>75 && q<=125)

{

c=750.0/100.0*q;

System.out.println(“TAX =”+c);

}

else if(q>125 && q<=200)

{

c=1225.0/100.0*q;

System.out.println(“TAX =”+c);

}

else if(q>200 && q<=350)

{

c=1650.0/100.0*q;

System.out.println(“TAX =”+c);

}

else

{

c=2000.0/100.0*q;

System.out.println(“TAX =”+c);

}

}

}

Sample Input:      25

Sample Output: NO TAX

12.
Write a program to input the code of a particular item, the quantity purchased and the rate. Then ask if any more items are to be purchased. If not, then calculate the total purchase price and print it along with the gift to be presented. The gifts to the customers are given in the following manner:

Amount of Purchase                                               Gift

100 and above but less than 500                           A key ring

500 and above but less than 1000                        A leather purse

1000 and above                                                     A pocket calculator

Before ending the program a “Thank you” statement should be printed to all the customers.

import java.io.*;

public class Q12

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

char ch;int q,r;String str;

System.out.println(“Enter the name of item”);

str=in.readLine();

System.out.println(“Enter the quantity of item”);

q=Integer.parseInt(in.readLine());

System.out.println(“Enter the rate of item”);

r=Integer.parseInt(in.readLine());

System.out.println(“Do you want to purchase any more item. press ‘y’ to continue and ‘n’ to exit”);

ch=(char)in.read();

if(ch==’n’)

{

if(r>100 && r<500)

{

System.out.println(“Total Amount =”+r);

System.out.println(“your gift is A KEY RING”);

}

else if(r>500 && r<1000)

{

System.out.println(“Total Amount =”+r);

System.out.println(“your gift is A LEATHER PURSE”);

}

else if(r>1000)

{

System.out.println(“Total Amount =”+r);

System.out.println(“your gift is A POCKET CALCULATOR”);

}

}

System.out.println(“THANK YOU”);

}

}

Sample Input:     Rice, 25, 1250, n

Sample Output:  Total Amount =1250

your gift is A POCKET CALCULATOR

THANK YOU

13.
A library charges a fine for books returned late as given below:

First five days                        40 paise per day

Six to ten days                       65 paise per day

Above ten days                     80 paise per day

Design a program in JAVA (with relevant supporting material) to calculate the fine assuming that a book is returned N days late.

import java.io.*;

public class Q13

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int n;double c;

System.out.println(“Enter the number of days”);

n=Integer.parseInt(in.readLine());

if(n<=5)

{

c=40.0/100.0*n;

System.out.println(“Amount to be paid =”+c);

}

else if(n>5 && n<=10)

{

c=(n-5)*65.0/100.0+40.0/100.0*5;

System.out.println(“Amount to be paid =”+c);

}

else

{

c=(n-10)*80.0/100.0+65.0/100.0*5+40.0/100.0*5;

System.out.println(“Amount to be paid =”+c);

}

}

}

Sample Input:      15

Sample Output:   Amount to be paid =9.25

14.
Mr. A. P. Singh is a software engineer. He pays annual income tax as per the given table:

Annual Salary                                               Rate Of income tax



Up to Rs. 100000                                         No Tax



Rs.100001 to Rs. 150000                           10% of amount exceeding Rs. 100000



Rs. 150001 to Rs. 250000                           Rs. 5000 + 20 % of amount exceeding  

                      Rs. 150000            



Above Rs. 250000                                         Rs. 25000 + 30 % of amount exceeding

                                                                            Rs.250000

Write a program in Java to compute the income tax to be paid by him.

public class Q14

{

public static void main (int sal)

{

double t;

{

if(sal<=100000)

t=0;

else if(sal>100000 && sal<=150000)

t=10.0/100.0*(sal-100000);

else if(sal>150000 && sal<=250000)

t=5000+20.0/100.0*(sal-150000);

else

t=25000+30.0/100.0*(sal-250000);

}

System.out.println(“Income tax = “+t);

}

}

Sample Input:         250000

Sample Output:      Income tax = 25000.0

15.
St. Xavier School displays a notice on the school notice board regarding admission in Std. XI for choosing different streams, according to marks obtained in English, Maths and Science in ICSE Examinations.

Marks obtained in diff. subjects                             Stream

Eng., Maths and Science >= 80%                                      Pure Science

Eng.and Science >= 80%,Maths >= 60%                         Bio. Science

Eng., Maths and Science >= 60%                                       Commerce

Print the appropriate stream allotted to the candidate. Write a program in Java to accept marks in English, Maths and Science from the console.

import java.io.*;

public class Q15

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

double e,m,sc,p,p1;

System.out.println(“Enter the marks in english”);

e=Double.parseDouble(in.readLine());

System.out.println(“Enter the marks in maths”);

m=Double.parseDouble(in.readLine());

System.out.println(“Enter the marks in science”);

sc=Double.parseDouble(in.readLine());

p=(e+m+sc)/3.0;

p1=(e+sc)/2.0;

if(p>=80)

{

System.out.println(“Eligiable for pure science”);

}

else if(p1>=80 && m>=60)

{

System.out.println(“Eligiable for bio science”);

}

else if(p>=60)

{

System.out.println(“Eligiable for commerce”);

}

else

{

System.out.println(“Not eligiable”);

}

}

}

Sample Input:      99, 100, 96

Sample Output:  Eligiable for pure science

16.
Enter the record of the employees of a company as employee number, age and basic salary. Write a program to calculate monthly gross salary and net salary of each employee as follows:



Dearness Allowance                                                Nature

House Rent Allowance                                           40% of Basic Salary

Provident Fund                                                         12% of Basic Salary

Employee Provident Fund                                     2% of Basic Salary

Gross Salary = Basic + DA + HRA

Net Salary = Gross Salary – (PF + EPF)

The program further checks whether an employee is an Income Tax payer or not, as under:



Category        Annual Gross Salary

Male (M)           Up to Rs. 1,50,000                      Income Tax Payer

Female (F)         Up to Rs. 2,00,000                     Not an Income Tax Payer

Enter ‘M’ or ‘F’ to check the category to decide whether an employee has to pay Income Tax or not. Print employee number, gross salary, net salary and also an ‘Income Tax’ payer or not.

import java.io.*;

public class Q16

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int bs,age,no;char ch;

double HRA,PF,EPF,DA,gs,ns,ags;

System.out.println(“Enter the employee number”);

no=Integer.parseInt(in.readLine());

System.out.println(“Enter the employee age”);

age=Integer.parseInt(in.readLine());

System.out.println(“Enter the employee’s basic salary”);

bs=Integer.parseInt(in.readLine());

HRA=40.0/100.0*bs;

PF=12.0/100.0*bs;

EPF=2.0/100.0*bs;

DA=45.0/100.0*bs;

gs=bs+DA+HRA;

ns=(gs-(PF+EPF));

ags=gs*12;

System.out.println(“Employee no. =”+no);

System.out.println(“Employee age =”+age);

System.out.println(“basic salary=”+bs);

System.out.println(“gross salary=”+gs);

System.out.println(“net salary=”+ns);

System.out.println(“Enter m for male and f for female to check whether a tax payer or not”);

ch=(char)System.in.read();

if(ch==’m’)

{

if(ags<=150000)

{

System.out.println(“Not an Income tax payer”);

}

else

{

System.out.println(“Income tax payer”);

}

}

else if(ch==’f’)

{

if(ags<=200000)

{

System.out.println(“Not an Income tax payer”);

}

else

{

System.out.println(“Income tax payer”);

}

}

}

}

Sample Input:    01, 25, 35000, m

Sample Output:   Employee no. =1

Employee age =25

basic salary=35000

gross salary=64750.0

net salary=59850.0

Income tax payer

17.
‘Bank of India’ announces new rates for Term Deposit Schemes for their customers and Senior Citizens as given below:

Term                     R% (General)           R%(Senior Citizen)

Up to 1 year                             7.5%                                      8.0%

Up to 2 years                            8.5%                                     9.0%

Up to 3 years                            9.5%                                     10.0%

More than 3 years                 10.0%                                    11.0%

The senior citizen is applicable to those customers, whose age is 60 years or more. Write a program to accept the sum (p) in term deposit scheme, age of the customer and the term. The program displays the information in the given format:

Amount Deposited     Term       Age       Interest earned        Amount Paid

Xxx                xxx     xxx              xxx                                         xxx

import java.io.*;

public class Q17

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int p,t,age;double a,si;

System.out.println(“Enter the sum”);

p=Integer.parseInt(in.readLine());

System.out.println(“Enter the term”);

t=Integer.parseInt(in.readLine());

System.out.println(“Enter the age”);

age=Integer.parseInt(in.readLine());

if(age<60)

{

if(t==1)

{

a=p*Math.pow((1+7.5/100.0),1);

si=a-p;

}

else if(t==2)

{

a=p*Math.pow((1+8.5/100.0),2);

si=a-p;

}

else if(t==3)

{

a=p*Math.pow((1+9.5/100.0),3);

si=a-p;

}

else

{

a=p*Math.pow((1+10.0/100.0),t);

si=a-p;

}

}

else

{

if(t==1)

{

a=p*Math.pow((1+8.0/100.0),1);

si=a-p;

}

else if(t==2)

{

a=p*Math.pow((1+9.0/100.0),2);

si=a-p;

}

else if(t==3)

{

a=p*Math.pow((1+10.0/100.0),3);

si=a-p;

}

else

{

a=p*Math.pow((1+11.0/100.0),t);

si=a-p;

}

}

System.out.println(“Amount Deposited\tTerm\tAge\tInterest Earned\tAmount Paid”);

System.out.println(+p+”\t”+t+”\t”+age+”\t”+si+”\t”+a);

}

}

Sample Input:                3500, 2, 65

Sample Output:   Amt Depo.       Term         Age     Int.  Earned       Amt Paid

3500                 2                 65            658.35            4158.35

18.
A special two-digit number is such that when the sum of its digits is added to the product of its digits, the result is equal to the original two-digit number.

Example: Consider the number 59.

Sum of digits = 5 + 9 = 14

Product of digits = 5 9 = 45

Total of the sum of digits and product of digits = 14 + 45 = 59

Write a program to accept a two-digit number. Add the sum of its digits to the product of its digits. If the value is equal to the number input, output the message “Special 2 — digit number” otherwise, output the message “Not a special two-digit number”.

import java.io.*;

public class Q18

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int n,dig1,dig2,sum,prod,sum2;

System.out.println(“Enter the two digit number”);

n=Integer.parseInt(in.readLine());

dig1=n/10;

dig2=n%10;

sum=dig1+dig2;

prod=dig1*dig2;

sum2=sum+prod;

if(sum2==n)

System.out.println(“Special two digit number”);

else

System.out.println(“Not a special two digit number”);

}

}

Sample Input:        25

Sample Output:      Not a special two digit number

19.
Using Switch Case, write a program that allows to accept any values from 1 to 7 and displays the weekdays corresponding to the number entered from the keyboard.

import java.io.*;

public class Q19

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

int n;

System.out.println(“Enter your choice:- 1 For 1st day of week and likewise”);

n=Integer.parseInt(in.readLine());

switch(n)

{

case 1:

System.out.println(“Sunday”);

break;

case 2:

System.out.println(“Monday”);

break;

case 3:

System.out.println(“Tuesday”);

break;

case 4:

System.out.println(“Wednesday”);

break;

case 5:

System.out.println(“Thursday”);

break;

case 6:

System.out.println(“Friday”);

break;

case 7:

System.out.println(“Saturday”);

break;

default:

System.out.println(“wrong choice”);

}

}

}

Sample Input:       2

Sample Output:    Monday

20.
Write a menu driven program to find the sum, difference and product of two numbers. Perform the tasks accordingly;

Enter ‘+’ to find the sum, to find the difference and ‘*’ to find the product of two numbers.

import java.util.Scanner;

public class Q20

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

int num1,num2,s,d=0,prod;char ch;

System.out.println(“Enter the two numbers”);

num1=in.nextInt();

num2=in.nextInt();

System.out.println(“Enter your choice, + for sum and likewise”);

ch=in.next().charAt(0);

switch(ch)

{

case ‘+’:

s=num1+num2;

System.out.println(“Sum of two numbers =”+s);

break;

case ‘-‘:

if(num1>num2)

{

d=num1-num2;

System.out.println(“Difference of two numbers =”+d);

}

else if(num2>num1)

{

d=num2-num1;

}

System.out.println(“Difference of the two numbers =”+d);

break;

case ‘*’:

prod=num1*num2;

System.out.println(“product of two numbers =”+prod);

break;

default:

System.out.println(“Wrong choice”);

}

}

}

Sample Input:       25, 50, +

Sample Output:   Sum of two numbers =75

21.
Using a switch case statement, write a menu driven program to convert a given temperature from Fahrenheit to Celsius and vice versa. For an incorrect choice, an appropriate error message should be displayed.

import java.util.Scanner;

public class Q21

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

double temp,f,c; char ch;

System.out.println(“Enter the temperature”);

temp=in.nextDouble();

System.out.println(“Press f to covert it to fahrenheit and c to celcius”);

ch=in.next().charAt(0);

switch(ch)

{

case ‘f’:

f=1.8*temp+32;

System.out.println(“output temp=”+f);

break;

case ‘c’:

c=(5.0/9.0*(temp-32));

System.out.println(“output temp=”+c);

break;

default:

System.out.println(“Wrong choice”);

}

}

}

Sample Input:          67, f

Sample Output:       temp=152.60000000000002

22.
Write a menu driven program to calculate:

Area of a circle = πr2
Area of a square = side*side
Area of a rectangle = length*breadth
Enter ‘c’ to calculate area of circle,’s’ to calculate area of square and ‘r’ to calculate area of rectangle.

import java.util.Scanner;

public class Q22

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

char ch;double r,a,s,l,b;

System.out.println(“Enter ‘c’ for area of circle and ‘s’ for square and ‘r’for rectangle”);

ch=in.next().charAt(0);

switch(ch)

{

case ‘c’:

System.out.println(“Enter the radius of circle”);

r=in.nextDouble();

a=22.0/7.0*r*r;

System.out.println(“Area=”+a);

break;

case ‘s’:

System.out.println(“Enter the side of square”);

s=in.nextDouble();

a=s*s;

System.out.println(“Area=”+a);

break;

case ‘r’:

System.out.println(“Enter the length and breadth of circle”);

l=in.nextDouble();

b=in.nextDouble();

a=l*b;;

System.out.println(“Area=”+a);

break;

default:

System.out.println(“Wrong choice”);

}

}

}

Sample Input:           c, 25

Sample Output:        Area=1964.2857142857142

23.
Write a menu driven program that outputs the results of the following evaluations 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) Random numbers between 0 and 1.

import java.util.Scanner;

public class Q23

{

public static void main(String args[])

{

Scanner in =new Scanner (System.in);

int num,c;double nlog,av,sq,rv;

System.out.println(“Enter the number”);

num=in.nextInt();

System.out.println(“Enter 1 to find log, 2 to absolute value, 3 to sqrt and 4 to random num between 0 and1”);

c=in.nextInt();

switch(c)

{

case 1:

nlog=Math.log(num);

System.out.println(“Natural logithirm=”+nlog);

break;

case 2:

av=Math.abs(num);

System.out.println(“Absolute value=”+av);

break;

case 3:

sq=Math.sqrt(num);

System.out.println(“Square root=”+sq);

break;

case 4:

rv=Math.random();

System.out.println(“Random numbers between 0 and 1=”+rv);

break;

default:

System.out.println(“Wrong choice”);

}

}

}

Sample Input:           90, 1

Sample Output:        Natural logithirm=4.499809670330265

24.
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.

Volume of a cube
Volume of a sphere
Volume of a cuboid
import java.util.Scanner;

public class Q24

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

int c;double s,v,r,l,b,h;

System.out.println(“Enter 1 to find volume of cube, 2 to sphere and 3 to cuboid”);

c=in.nextInt();

switch(c)

{

case 1:

System.out.println(“Enter the side of the cube”);

s=in.nextDouble();

v=s*s*s;

System.out.println(“Volume of the cube=”+v);

break;

case 2:

System.out.println(“Enter the radius of sphere”);

r=in.nextDouble();

v=4.0/3.0*22.0/7.0*r*r*r;

System.out.println(“Volume of the cube=”+v);

break;

case 3:

System.out.println(“Enter the length, breadth and height of cuboid”);

l=in.nextDouble();

b=in.nextDouble();

h=in.nextDouble();

v=l*b*h;

System.out.println(“Volume of the cube=”+v);

break;

default:

System.out.println(“Wrong choice”);

}

}

}

Sample Input:      1, 8

Sample Output:   Volume of the cube=512.0

25.
A Promoter cum Developer announces a special bonanza for early booking of the flats for their customers as per the tariff given below:

Category           Discount on the price    Discount on Development Charge

Ground floor            10%                                           8%

First floor                   2.0%                                          1.0%

Second floor                5%                                           5%

Third floor                 7.5%                                          10%

Write a menu driven program to input price and the category ‘0’ (zero) for ground floor, ‘1’ for first floor, ‘2’ for second floor and ‘3’ for third floor). Calculate and display the total discount, price of the flat after getting discount.

import java.util.Scanner;

public class Q25

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

int c,p;double dp,ddc,td,pd;

System.out.println(“Enter the price”);

p=in.nextInt();

System.out.println(“Eneter 0 for ground floor, 1 gor first floor and likewise”);

c=in.nextInt();

switch(c)

{

case 0:

dp=10.0/100.0*p;

ddc=8.0/100.0*p;

td=dp+ddc;

pd=p-td;

System.out.println(“Total discount=”+td);

System.out.println(“Price to be paid=”+pd);

break;

case 1:

dp=2.0/100.0*p;

ddc=1.0/100.0*p;

td=dp+ddc;

pd=p-td;

System.out.println(“Total discount=”+td);

System.out.println(“Price to be paid=”+pd);

break;

case 2:

dp=5.0/100.0*p;

ddc=5.0/100.0*p;

td=dp+ddc;

pd=p-td;

System.out.println(“Total discount=”+td);

System.out.println(“Price to be paid=”+pd);

break;

case 3:

dp=7.5/100.0*p;

ddc=10.0/100.0*p;

td=dp+ddc;

pd=p-td;

System.out.println(“Total discount=”+td);

System.out.println(“Price to be paid=”+pd);

break;

default:

System.out.println(“wrong choice”);

}

}

}

Sample Input:       65000, 1

Sample Output:    Total discount=1950.0

Price to be paid=63050.0

26.
A company announces revised Dearness Allowance (DA) and Special Allowances (SA) for their employees as per the tariff given below:

Basic Dearness Allowance (DA) Special Allowance (SA)
Up to Z10,000 10% 5%
Z 10,001 – Z 20,000 12% 8%
Z 20,001 – Z 30,000 15% 10%
230,001 and above 20% 12%
Write a program to accept Basic Salary (BS) of an employee. Calculate and display gross salary.

Gross Salary = Basic + Dearness Allowance + Special Allowance.

Print the information in the given format:

Basic              DA                      Spl. Allowance                    Gross Salary

xxx               xxx                             xxx                                      xxx

import java.util.Scanner;

public class Q26

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

int bs;double DA,SA,gs;

System.out.println(“Enter the basic salary of employee”);

bs=in.nextInt();

if(bs<=10000)

{

DA=10.0/100.0*bs;

SA=50.0/100.0*bs;

gs=bs+DA+SA;

}

else if(bs>10000 && bs<=20000)

{

DA=12.0/100.0*bs;

SA=8.0/100.0*bs;

gs=bs+DA+SA;

}

else if(bs>20000 && bs<=30000)

{

DA=15.0/100.0*bs;

SA=10.0/100.0*bs;

gs=bs+DA+SA;

}

else

{

DA=20.0/100.0*bs;

SA=12.0/100.0*bs;

gs=bs+DA+SA;

}

System.out.println(“Basic\tDA\tSpl. Allowance\tGross Salary”);

System.out.println(+bs+”\t”+DA+”\t”+SA+”\t”+”\t”+gs);

}

}

Sample Input:         8000

Sample Output:      Basic     DA          Spl. Allowance         Gross Salary

8000     800.0           4000.0                    12800.0

27.
You want to buy an old car from ‘Sale and Purchase’, where you can get a car in depreciated price. The depreciation value of a car is calculated on its showroom price and the number of years it has been used. The depreciated value of a car is calculated as per the tariff given below:

No. of Years used                           Rate of Depreciation

1                                                       10%

2                                                      20%

3                                                       30%

4                                                        50%

Above 4 Years                                              60%

Write a menu driven program to input showroom price and the number of years used (`1′ for one year old, ‘2’ for two years old) and so on. Calculate the depreciated value. Display the original price of car, depreciated value and the amount paid for the car.

import java.util.Scanner;

public class Q27

{

public static void main(String args[])

{

Scanner in =new Scanner(System.in);

int sp,t;double rd,ap;

System.out.println(“Enter the showroom price of car”);

sp=in.nextInt();

System.out.println(“Enter the number of years it has been used”);

t=in.nextInt();

switch(t)

{

case 1:

rd=10.0/100.0*sp;

ap=sp-rd;

break;

case 2:

rd=20.0/100.0*sp;

ap=sp-rd;

break;

case 3:

rd=30.0/100.0*sp;

ap=sp-rd;

break;

case 4:

rd=50.0/100.0*sp;

ap=sp-rd;

break;

default:

rd=60.0/100.0*sp;

ap=sp-rd;

}

System.out.println(“Original price of the car=”+sp);

System.out.println(“Depreciated value=”+rd);

System.out.println(“Amount to be paid=”+ap);

}

}

Sample Input:      500000, 2

Sample Output:   Original price of the car=500000

Depreciated value=100000.0

Amount to be paid=400000.0

28.
An electronics shop has announced the following seasonal discounts on purchase of certain items.

Category
Up to Rs. 225,000

Discount on Laptop
0.0%

Discount on Desktop PC
5.0%

Rs. 25,001 – Rs.57,000 5.0% 7.5%
Rs. 57,001 – Rs.1,00,000 7.5% 10.0%
More than Rs. 1,00,000 10.0% 15.0%
Write a program based on above criteria, to input name, address, amount of purchase and the type of purchase (`L,’ for Laptop and ‘D’ for Desktop). Compute and print the net amount to be paid by a customer with his name and address.

import java.io.*;

public class Q28

{

public static void main(String args[])throws IOException

{

InputStreamReader read =new InputStreamReader(System.in);

BufferedReader in =new BufferedReader(read);

String name,add;int ap;double d,np;char ch;np=0;

System.out.println(“Enter the name of the customer”);

name=in.readLine();

System.out.println(“Enter the address of customer”);

add=in.readLine();

System.out.println(“Enter the amount of purchase”);

ap=Integer.parseInt(in.readLine());

System.out.println(“Enter l for laptop and d for desktop”);

ch=(char)System.in.read();

if(ch==’l’)

{

if(ap<=25000)

{

d=0.0;

np=ap;

}

else if(ap>25000 && ap<=57000)

{

d=5.0/100.0*ap;

np=ap-d;

}

else if(ap>57000 && ap<=100000)

{

d=7.5/100.0*ap;

np=ap-d;

}

else

{

d=10.0/100.0*ap;

np=ap-d;

}

}

else if(ch==’d’)

{

if(ap<=25000)

{

d=5.0;

np=ap;

}

else if(ap>25000 && ap<=57000)

{

d=7.5/100.0*ap;

np=ap-d;

}

else if(ap>57000 && ap<=100000)

{

d=10.0/100.0*ap;

np=ap-d;

}

else

{

d=15.0/100.0*ap;

np=ap-d;

}

}

System.out.println(“Name of the customer=”+name);

System.out.println(“Address of the customer=”+add);

System.out.println(“Price to be paid by the customer=”+np);

}

}

Sample Input:     YK, Kytimes street, 50000, l

Sample Output:  Name of the customer=YK

Address of the customer=Kytimes street

Price to be paid by the customer=47500.0

No comments:

Post a Comment

any problem in any program comment:-

Second largest number in java using ternary operator

 //Largest second number using ternary operator public class Main { public static void main(String[] args) { int a=5,b=6,c=7; int ...