0% found this document useful (0 votes)
68 views

Answer key 1st Assignment (Conditional Statement)

Uploaded by

antarikhyag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
68 views

Answer key 1st Assignment (Conditional Statement)

Uploaded by

antarikhyag
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

CONDITIONAL STATEMENT ASSIGNMENT

1. Write a program that inputs the number of sides of a polygon and then determines whether it is a
hexagon, a pentagon, a rectangle and a triangle.

import java.util.*;
class Polygon
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of sides:");
int s = sc.nextInt();
switch(s)
{
case 3:
System.out.println("It's a Triangle");
break;
case 4:
System.out.println("It's a Rectangle");
break;
case 5:
System.out.println("It's a Pentagon");
break;
case 6:
System.out.println("It's a Hexagon");
break;
default:
System.out.println("Invalid Input");
}
}
}

2. Write a program in java to input the weight of the parcel and compute charges for sending
parcels when are as follows:
For the first 1kg: ₹ 15.00
For additional weight, for every 500 gm or fraction thereof: ₹ 8.00

import java.util.*;
class Parcel
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the weight of the parcel in gms:");
int w = sc.nextInt();
if(w <= 1000)
{
System.out.println("The charges for the parcel is Rs.15");
}
else
{
double ch = (double)(w - 1000)/500;
ch = 15 + ch * 8;
System.out.println("The charges for the parcel is Rs." + ch);
}
}
}

3. Write a menu driven program to calculate:


1) Area of a circle (π r2)
2) Area of square (side * side)
3) Area of rectangle (length * breadth)

import java.util.*;
class Area
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Area of a circle");
System.out.println("Enter 2 for Area of a square");
System.out.println("Enter 3 for Area of a rectangle");
System.out.println("Enter your choice:");
int ch = sc.nextInt();
double ar=0.0;
switch(ch)
{
case 1:
System.out.println("Enter the radius");
double r = sc.nextDouble();
ar = 3.14 * r * r;
System.out.println("Area of a circle is " + ar);
break;
case 2:
System.out.println("Enter the side");
double s = sc.nextDouble();
ar = s * s;
System.out.println("Area of a square is " + ar);
break;
case 3:
System.out.println("Enter the length");
double l = sc.nextDouble();
System.out.println("Enter the length");
double b = sc.nextDouble();
ar = l * b;
System.out.println("Area of a rectangle is " + ar);
break;
default:
System.out.println("Invalid Input");
}
}
}

4. Write a menu driven program to find the area of an Equilateral triangle, an Isosceles
triangle and a scalene triangle as per the user’s choice.
√3 2
a. Equilateral triangle = 𝑠 , s = side of an equilateral triangle
4
1
b. Isosceles triangle = 4 ∗ 𝑏 ∗ √4𝑎2 − 𝑏 2
𝑚+𝑛+𝑝
c. Scalene triangle = √𝑠 ∗ (𝑠 − 𝑚) ∗ (𝑠 − 𝑛) ∗ (𝑠 − 𝑝) , 𝑠 = 2
(where m, n and p are three sides of a triangle)
import java.util.*;
class Triangle
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter 1 for Area of a Equilateral Triangle");
System.out.println("Enter 2 for Area of a Isosceles Triangle");
System.out.println("Enter 3 for Area of a Scalene Triangle");
System.out.println("Enter your choice:");
int ch = sc.nextInt();
double ar=0.0;
switch(ch)
{
case 1:
System.out.println("Enter the side");
double si = sc.nextDouble();
ar = Math.sqrt(3)/4 * si * si;
System.out.println("Area of a Equilateral Triangle is " + ar);
break;
case 2:
System.out.println("Enter the value of a ");
double a = sc.nextDouble();
System.out.println("Enter the value of b ");
double b = sc.nextDouble();
ar = 0.25 * b * (Math.sqrt(4*a*a - b*b));
System.out.println("Area of a Isosceles Triangle is " + ar);
break;
case 3:
System.out.println("Enter the m");
double m = sc.nextDouble();
System.out.println("Enter the n");
double n = sc.nextDouble();
System.out.println("Enter the p");
double p = sc.nextDouble();
double s = ((m+n+p)/2);
ar = (Math.sqrt(s*(s-m)*(s-n)*(s-p)));
System.out.println("Area of a Scalene Triangle is " + ar);
break;
default:
System.out.println("Invalid Input");
}
}
}

5. A school displays a notice board regarding admission in class XI for choosing different streams
according to the marks obtained in English, Mathematics and Science in ICSE Examinations.
Marks obtained Stream
Eng, Maths and science >= 80 Science with Computer
Science >= 80 Eng and Maths >=60 Science with Biology
Science < 80 Eng and Maths >=70 Commerce
Science, Eng and Maths >=50 Arts
Write a program to accept marks of English, Mathematics and Science. Display the Stream
appropriate stream.

import java.util.*;
class Stream
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the marks of English:");
int e = sc.nextInt();
System.out.println("Enter the marks of Mathematics:");
int m = sc.nextInt();
System.out.println("Enter the marks of Science:");
int s = sc.nextInt();
System.out.print("Stream is ");
if(e>=80 && m>=80 && s>=80)
System.out.println("Science with Computer");
else if(e>=60 && m>=60 && s>=80)
System.out.println("Science with Biology");
else if(e>=70 && m>=70 && s<80)
System.out.println("Commerce");
else if(e>=50 && m>=50 && s>=50)
System.out.println("Arts");
}
}

6. Write a program that offers a menu to choose from various categories of cinema hall tickets.
After the user selects a category, ask for number of tickets the user wants to buy and then display
the amount to be paid. The price list are as follows:
Category Price
Premium ₹ 150
Executive ₹ 250
Gold ₹ 350

import java.util.*;
class Movie
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Select Category of ticket");
System.out.println("Enter P for Premium");
System.out.println("Enter E for Executive");
System.out.println("Enter G for Gold");
char ch = sc.next().charAt(0);
System.out.println("Enter the no. of Tickets:");
int t = sc.nextInt();
int amt = 0;
switch(ch)
{
case 'P':
case 'p':
amt = 150 * t;
break;
case 'E':
case 'e':
amt = 250 * t;
break;
case 'G':
case 'g':
amt = 350 * t;
break;
default:
System.out.println("Entered Category is not applicable");
System.exit(0);
}
System.out.println("The amount of the Tickets is " + amt);
}
}

7. A Salesman is paid a commission on the following basis:


Sales ( ₹ ) Rate of commission (%)
Up to ₹ 10000 2%
₹ 10001 – ₹ 20000 3%
₹ 20001 – ₹ 50000 5%
₹ 50001 – ₹ 70000 9%
Above ₹ 70000 12 %
Write a program to accept the sales amount and calculate the commission. Print the sales and
amount of commission.

import java.util.*;
class Commision
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the sales amount:");
int s = sc.nextInt();
double c =0.0;
if(s<=10000)
c = 0.02 * s;
else if(s>=10001 && s<=20000)
c = 0.03 * s;
else if(s>=20001 && s<=50000)
c = 0.05 * s;
else if(s>=50001 && s<=70000)
c = 0.09 * s;
else if(s>=10001 && s<=20000)
c = 0.12 * s;
System.out.println("The sales is " + s);
System.out.println("The amount of commission is " + c);
}
}

8. 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 ₹ / Gallons
Up to 45 No tax
More than 45 but 75 or less 50
More than 75 but 125 or less 75
More than 125 but 200 or less 100
More than 200 but 350 or less 125
More than 350 150
Write a program to accept the consumed amount in gallon to compute and display the annual
water tax based on the chart.

import java.util.*;
class Conservation
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the amount of water in Gallons:");
int w = sc.nextInt();
double amt =0.0;
if(w<=45)
amt = 0.0;
else if(w>=45 && w<=75)
amt = w * 50;
else if(w>=75 && w<=125)
amt = w * 75;
else if(w>=125 && w<=200)
amt = w * 100;
else if(w>=200 && w<=350)
amt = w * 125;
else if(w>=350)
amt = w * 150;
System.out.println("The water tax is " + amt);
}
}

9. Mr. Paul is a mechanical engineer. He pays annual income tax as per the given table:
Annual salary Rate of income tax
Up to ₹ 100000 No tax
₹ 100001 to ₹ 150000 10% of the amount exceeding ₹100000
₹ 150001 to ₹ 250000 ₹ 5000 + 20% of the amount exceeding ₹ 150000
Above ₹ 250000 ₹ 20000 + 30% of the amount exceeding ₹ 250000
Write a program in Java to compute the income tax to be paid by him.

import java.util.*;
class IncomeTax
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the annual income:");
int a = sc.nextInt();
double t =0.0;
if(a<=100000)
t = 0.0;
else if(a>=100001 && a<=150000)
t = 0.10 * (a-100000);
else if(a>=150001 && a<=250000)
t = 5000 + 0.20 * (a-150000);
else if(a>=250001)
t = 20000 + 0.30 * (a-250000);
System.out.println("The income tax to be paid is " + t);
}
}

10. The West Bengal Electricity Board (WBEB) charges from the consumers according to the units
consumed per year but the amount is to be paid quarterly as per the given tariff:
Units consumed Charges
Up to 1500 units ₹ 4.50 /unit
For next 1000 units ₹ 5.50 /unit
More than 2500 units ₹ 7.50 /unit
Write a program to input consumer’s name, consumer number and the units consumed. The
program displays the following information at the time of receiving the money receipt as:
West Bengal Electricity Board
Money Receipt
Consumer number:
Consumer’s name:
Units consumed:
Yearly amount:
Amount to be paid quarterly:

import java.util.*;
class WBEB
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the Consumer Name:");
String nm = sc.nextLine();
System.out.println("Enter the Consumer number:");
int no = sc.nextInt();
System.out.println("Enter the unit consumed:");
int u = sc.nextInt();
double y =0.0,q=0.0,a =0.0;
if(u<=1500)
a = u * 4.50;
else if(u>=1501 && u<=2500)
a = (1500 * 4.50) + (5.50 * (u-1500));
else if(u>2500)
a = (1500 * 4.50) + (5.50 * 1000) + (7.50 * (u - 2500));
y = a*12;
q = y/4;
System.out.println("\t\tWest Bengal Electricity Board");
System.out.println("\t\t\t\t\tMoneyReceipt");
System.out.println("Consumer Number:" + no);
System.out.println("Consumer Name:" + nm);
System.out.println("Units Consumed:" + u);
System.out.println("Yearly Amount:" + y);
System.out.println("Amount to be paid quaterly:" + q);
}
}

11. ‘Bank of India’ announces new rates for term deposit schemes for their customers and Senior
Citizens as given below:
Rate of Interest Rate of Interest
Term
(General) (Senior Citizen)
Up to 1 year 7.5% 8.0%
Up to 2 years 8.5% 9.0%
Up tp 3 years 9.5% 10.0%
More than 3 years 10.0% 11.0%
The ‘senior citizen’ tag is applicable to the 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


xxxx xxxx xx xxxx xxxx

import java.util.*;
class Bank
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the principal amount:");
int p = sc.nextInt();
System.out.println("Enter the age of the customer:");
int a = sc.nextInt();
System.out.println("Enter the term:");
int t = sc.nextInt();
double i = 0.0, amt = 0.0;
if(t>0)
{
switch(t)
{
case 1:
if(a>=60)
i = p * 0.08;
else
i = p * 0.075;
break;
case 2:
if(a>=60)
i = p * 0.09;
else
i = p * 0.085;
break;
case 3:
if(a>=60)
i = p * 0.10;
else
i = p * 0.095;
break;
default:
if(a>=60)
i = p * 0.10;
else
i = p * 0.11;
}
amt = i + p;
}
System.out.println("Amount Deposited \t Term \t Age \t Interest Earned \t Amount Paid");
System.out.println(p+"\t\t\t "+t+"\t "+a+"\t "+i+"\t\t\t\t\t "+amt);
}
}

12. A hotel is giving a seasonal discount on the total discount to be paid by the customer. The
charges for different rooms are given below:
Category Tariff
Semi Deluxe Room ₹ 2500 /- per day
Deluxe Room ₹ 3500 /- per day
Super Deluxe Room ₹ 5000 /- per day

The discount will be given as per the following criteria:


No. of days stayed Discount
Up to 5 days 10 %
More than 5 days and up to 10 days 20 %
More than 10 days 30 %
Write a program to input the name of the customer, category of the room (‘S’ for semi Deluxe,
‘D’ for Deluxe, ‘SD’ for Super Deluxe) and the number of days stayed in the hotel. Calculate the
discount and the total amount to be paid. Print the bill along with the name.

import java.util.*;
class Hotel
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the customer:");
String nm = sc.nextLine();
System.out.println("Enter s for Semi Deluxe Room:");
System.out.println("Enter d for Semi Deluxe Room:");
System.out.println("Enter sd for Super Deluxe Room:");
System.out.println("Enter the Category:");
String c = sc.nextLine();
System.out.println("Enter the no.of days:");
int d = sc.nextInt();
double dis = 0.0, amt = 0.0;
if(d <= 5)
dis = 0.10;
else if(d>=5 && d<=10)
dis = 0.20;
else if(d>10)
dis = 0.30;
switch(c)
{
case "S":
case "s":
amt = 2500 * d;
dis = dis * amt;
amt = amt - dis;
break;
case "D":
case "d":
amt = 3500 * d;
dis = dis * amt;
amt = amt - dis;
break;
case "SD":
case "sd":
case "sD":
case "Sd":
amt = 5000 * d;
dis = dis * amt;
amt = amt - dis;
break;
default:
System.out.println("Invalid Input");
}
System.out.println("Customer's Name:" + nm);
System.out.println("Category of room:" + c);
System.out.println("Number of Days stayed in the hotel:" + d);
System.out.println("Discount given:" + dis);
System.out.println("Total amount to be paid:" + amt);
}
}

13. Enter the record of the employees of a country as employee number, name, age, gender (Enter
‘M’ or ‘F’) and the basic salary. Write a program to calculate monthly gross salary and net salary
of the employee as follows:
DA = 40% of the basic HRA = 20% of the basic
TA = 10% of the basic PF = 12% of the basic
Gross Salary = Basic + DA + HRA + TA
Net Salary = Gross Salary – PF
The program further checks whether an employee is an Income Tax payer or not, as under:
Print the employee number, name, age, gross salary net salary and also a income tax payer or not,
as under:
Category Annual gross salary
Up to ₹ 2,50,000 No Tax
Male (M)
Above ₹ 2,50,000 10% of the annual gross salary
Up to ₹ 3,00,000 No tax
Female (F)
Above ₹ 3,00,000 10% of the annual gross salary

import java.util.*;
class Employee
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter the name of the employee:");
String nm = sc.nextLine();
System.out.println("Enter the employee's number:");
int eno = sc.nextInt();
System.out.println("Enter the employee's age:");
int a = sc.nextInt();
System.out.println("Enter the gender:");
char g = sc.next().charAt(0);
System.out.println("Enter the salary:");
int bs = sc.nextInt();
double da = bs * 0.40;
double hra = bs * 0.20;
double ta = bs * 0.10;
double pf = bs * 0.12;
double gross = bs + da + hra + ta;
double net = gross - pf;
System.out.println("Employee's Number:" + eno);
System.out.println("Employee's Name:" + nm);
System.out.println("Employee's Age:" + a);
System.out.println("Gross salary:" + gross);
System.out.println("Net salary:" + net);
double t = 0.0;
switch(g)
{
case 'M':
case 'm':
if((gross*12) <=250000)
System.out.println("No Tax");
else
{
t = (gross*12) * 0.10;
System.out.println("Tax is "+ t);
}
break;
case 'F':
case 'f':
if((gross*12) <=300000)
System.out.println("No Tax");
else
{
t = (gross*12) * 0.10;
System.out.println("Tax is "+ t);
}
break;
default:
System.out.println("Invalid gender input");
}
}
}

14. A transport company charges for carrying parcels as per city and the weight. The tariff is given
below:
City Weight Charges
Up to 100 kg ₹ 45 /kg
Kolkata
More than 100 kg ₹ 75 /kg
Up to 100 kg ₹ 65 /kg
Mumbai
More than 100 kg ₹ 95 /kg
Up to 100 kg ₹ 75 /kg
Chennai
More than 100 kg ₹ 115 /kg
Up to 100 kg ₹ 90 /kg
Delhi
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 the customer for
booking.

import java.util.*;
class Company
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter K for Kolkata");
System.out.println("Enter M for Mumbai");
System.out.println("Enter C for Chennai");
System.out.println("Enter D for Delhi");
System.out.println("Select City");
char ch = sc.next().charAt(0);
System.out.println("Enter the weight:");
int w = sc.nextInt();
int c = 0;
switch(ch)
{
case 'K':
case 'k':
if(w<=100)
c = w * 45;
else
c = w * 75;
break;
case 'M':
case 'm':
if(w<=100)
c = w * 65;
else
c = w * 95;
break;
case 'C':
case 'c':
if(w<=100)
c = w * 75;
else
c = w * 115;
break;
case 'D':
case 'd':
if(w<=100)
c = w * 90;
else
c = w * 125;
break;
default:
System.out.println("Invalid Input");
}
System.out.println("The charges to be paid is "+ c);
}
}

15. Using Switch statement, write a menu driven program to calculate the maturity amount
of a bank deposit. The user is given the following options:
a. Term Deposit
b. Recurring Deposit
For option (a) accept principal (p), rate of interest (r) and time period in year (n).
Calculate and output the maturity amount (A) receivable using the formula:
𝑟 𝑛
𝐴 = 𝑝(1 + )
100
For option (b) accept monthly instalment (p), rate of interest (r) and time period in
months (n). Calculate and output the maturity amount (A) receivable using the
formula:
[𝑛 ∗ (𝑛 + 1)] 𝑟 1
𝐴=𝑝∗𝑛+𝑝∗ ∗ ∗
2 100 12
For an incorrect option, an appropriate error message should be displayed.

import java.util.*;
class Deposit
{
public static void main()
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter a for Term Deposit");
System.out.println("Enter b for Recurring Deposit");
System.out.println("Enter your choice:");
char ch = sc.next().charAt(0);
double a = 0.0,r;
int p,n;
switch(ch)
{
case 'a':
case 'A':
System.out.println("Enter the principal");
p = sc.nextInt();
System.out.println("Enter the rate of interest ");
r = sc.nextDouble();
System.out.println("Enter the time period in year");
n = sc.nextInt();
a = p * (Math.pow(( 1 + (r/100)),n));
System.out.println("Maturity amount is "+a);
break;
case 'b':
case 'B':
System.out.println("Enter the principal");
p = sc.nextInt();
System.out.println("Enter the rate of interest ");
r = sc.nextFloat();
System.out.println("Enter the time period in month");
n = sc.nextInt();
a = p * n + p * ((n*(n+1))/2) * (r/100) * (0.083);
System.out.println("Maturity amount is "+a);
break;
default:
System.out.println("Invalid Input");
}
}
}

You might also like