Professional Documents
Culture Documents
SOLVED PROGRAMS
CHAPTER: CONDITIONAL STATEMENT
import java.util.Scanner;
public class Great
{
public static void main(String args[])
{
Scanner sc=new Scanner (System.in);
int a,b;
System.out.println("enter 1st nos:");
a=sc.nextInt();
System.out.println("enter 2nd nos:");
b=sc.nextInt();
if(a>b)
{
System.out.println("a is greatest");
}
else
{
System.out.println("b is greatest");
}
}}
Program 2. Write a program to input three unequal and display the second
smallest number.
import java.util.Scanner;
public class Smallest
{
public static void main(String args[])
{
Scanner in=new Scanner(System.in);
int a,b,c;
System.out.println("Enter three numbers");
a=in.nextInt();
b=in.nextInt();
c=in.nextInt();
if (a<b)
{
if(b<c)
System.out.println("The second smallest number:"+b);
else
System.out.println("The second smallest number:"+c);
}
if(b<c)
{
if(c<a)
System.out.println("The second smallest number:"+c);
else
System.out.println("The second smallest number:"+a);
}
if(c<a)
{
if(a<b)
System.out.println("The second smallest number:"+a);
else
System.out.println("The second smallest number:"+b);
}}}
import java.util.*;
class grade
{
public static void main(String args[]){
Scanner sc=new Scanner(System.in);
System.out.println("enter marks");
int m=sc.nextInt();
if (m>=90)
System.out.println("Grade=A");
else if (m>=70)
System.out.println("Grade=B");
else if (m>=50)
System.out.println("Grade=C");
else
System.out.println("Grade=D");
}
}
Program 5. Write a program to input three integers and check whether it forms a
Pythagorean triplet or not. A set of three integers is said to be a Pythagoren triplet
if the sum of the squares of any two integers is equal to square of the third
integer. Example, (3, 4, 5), (5, 12, 13), and (7, 24, 25).
import java.util.*;
class P_Tpriplet
{
public static void main()
{
int a,b,c;
Scanner sc=new Scanner(System.in);
System.out.println("Enter 3 integers");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
if (a*a==b*b+c*c || b*b==a*a+c*c || c*c==a*a+b*b)
System.out.println("Pythagorean Triplet");
else
System.out.println("Not a Pythagorean triplet");
}}
Program 6. Write a program to input three angles of a triangle and check whether
a triangle is possible or not. If possible then check whether it is an acute-angled
triangle, right-angled or an obtuse-angled triangle otherwise, display 'Triangle
not possible'.
Sample Input: Enter three angles: 40, 50, 90
Sample Output: Right=angled Triangle
import java.util.Scanner;
import java.util.Scanner;
Program 8. Write program to accept the number of days and display it after
Converting into number of years, number of months and number of days
import java.util.*;
class Day
{
public static void main(String arg[])
{
Scanner sc=new Scanner(System.in);
System.out.println("enter no. of days");
int a=sc.nextInt();
int y=a/365;
int b=a%365;
int m=b/30;
int days=b%30;
System.out.println("no of year="+y);
System.out.println("no of month="+m);
System.out.println("no of days="+days);
}
}
Program 9 .An Electricity Company charges their consumers according to the
units consumed per month
According to the given traffic:
Units Consumed Charges
Up to 100 units Rs. 2 per unit
More than 100 units and up to 200 units Rs. 1.80 per unit
More than 200 units Rs.1.50 per unit
In addition to the above, every consumer has to pay Rs.200 as Service Charge
per month. Write a program to input the amount of units consumed and
calculate the total charges payable (Bill) by the consumer.
import java.util.*;
class electricity_bill
{
public static void main(String args[])
{
int u;double c=0.0;double tc=0.0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter unit");
u=sc.nextInt();
if (u<=100){
c=2*u;}
else if (u>100&&u<=200){
c=100*2+(u-100)*1.8;}
else if(u>200){
c=100*2+100*1.8+(u-200)*1.50;}
tc=200+c;
System.out.println("Amount Payable="+tc);
}
}
Program 10. Write a program to accept a number and check whether the
number is divisible by 3 as well as 5. Otherwise, decide:
(a) Is the number divisible by 3 and not by 5?
(b) Is the number divisible by 5 and not by 3?
(c) Is the number neither divisible by 3 nor by 5?
The program displays the message accordingly.
import java.util.Scanner;
Program 11. A cloth showroom has announced festival discounts and the gifts on
the purchase of items, based on the total cost as given below:
import java.util.Scanner
}
}
Program 12. 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 actual
profit and profit %. Otherwise calculate and display actual loss and loss %.
If the cost price and the selling price are equal, then display the message
“Neither profit nor loss”.
import java.util.*;
class Cp_Sp{
public static void main(String args[]){
Scanner sc=new Scanner (System.in);
System.out.print("Cost Price = ");
double cp = sc.nextDouble();
System.out.print("Selling Price = ");
double sp = sc.nextDouble();
if(sp == cp)
System.out.println("Neither profit nor loss.");
else if(sp > cp){
double profit = sp - cp;
double pp = profit / cp * 100;
System.out.println("Profit = " + profit);
System.out.println("Profit % = " + pp);
}
else{
double loss = cp - sp;
double lp = loss / cp * 100;
System.out.println("Loss = " + loss);
System.out.println("Loss % = " + lp);
}
}
}
Program 13. Write a program to input two unequal positive numbers and check
whether
They are perfect square numbers or not. If the user enters a negative number
then the program displays the message 'Square root of a negative number can't
be determined'.
Sample Input: 81, 100
Sample Output: They are perfect square numbers.
Sample Input: 225, 99
Sample Output: 225 is a perfect square number.
99 is not a perfect square number.
import java.util.Scanner;
if (a < 0 || b < 0) {
System.out.println("Square root of a negative number can't be
determined");
}
else {
double sq1 = Math.sqrt(a);
double sq2= Math.sqrt(b);
double perfect1= sq1- Math.floor(sq1);
double perfect2= sq2 - Math.floor(sq2);
if (perfect1 == 0 && perfect2== 0) {
System.out.println("They are perfect square numbers.");
}
else {
System.out.println("Both are not perfect square numbers.");
}
}
}
}
Program 14. 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 paid by the passenger.
The program displays the printed bill with the details given below:
Taxi No. ________
Distance covered ________
Amount ________
import java.util.*;
class Taxi
{
public static void main(String args[])
{
int num, d;
double amt = 0.0;
Scanner sc=new Scanner(System.in);
System .out.println("enter the taxi no, distance:");
num=sc.nextInt();
d=sc.nextInt();
if(d <= 5)
amt = 100.0;
else if(d <= 15)
amt = 100.0 + (d - 5) * 10;
else if(d <= 25)
amt = 200.0 + (d - 15) * 8;
else if(d>25)
amt = 280.0 + (d - 25) * 5;
System.out.println("Taxi No: " + num);
System.out.println("Distance covered: " + d);
System.out.println("Amount: " + amt);
}
}
import java.util.*;
class CourierCompany
{
public static void main(String args[])
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter the weight of parcel");
int w = sc.nextInt();
System.out.println("Enter the type of booking" + "\n 1: O for Ordinary"
+ "\n 2: E for Express");
char t = sc.next().charAt(0);
int c=0;
if(t == 'O')
{
if(w<=100)
c=80;
else if(w>=101 && w<=500)
c=150;
else if(w>=501 && w<=1000)
c=210;
else if(w>1000)
c=250;
}
else if(t == 'E')
{
if(w<=100)
c=100;
else if(w>=101 && w<=500)
c=200;
else if(w>=501 && w<=1000)
c=250;
else if(w>1000)
c=300;
}
System.out.print("Charges = Rs " + c);
}
}
Program 16. Given below is a hypothetical table showing rate of income tax for
an India citizen, who is below or up to 60 years.
Write a program to input the name, age and taxable income of a person.
If the age is more than 60 years then display the message "Wrong Category".
If the age is less than or equal to 60 years then compute and display the income
tax payable along with the name of tax payer, as per the table given above.
import java.util.Scanner;
Write a program to accept name and 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:
Name Basic DA Spl. Allowance Gross Salary
xxx xxx xxx xxx xxx
import java.util.Scanner;
double gs = bs + da + sa;
System.out.println("Name\tBasic\tDA\tSpl. Allowance\tGross Salary");
System.out.println(name + "\t" + bs + "\t" + da + "\t" + sa + "\t" + gs);
}
}
Program 18. Mr. Kumar is an LIC agent. He offers discount to his policy holders
on
the annual premium. However, he also gets commission on the sum assured as
per the given tariff.
Write a program to input name of the policy holder, the sum assured and first
annual premium. Calculate the discount of the policy holder and the
commission of the agent. The program displays all the details as:
Name of the policy holder :
Sum assured :
Premium :
Discount on the first premium :
Commission of the agent :
import java.util.Scanner;
}
}
Program 19. An employee wants to deposit certain sum of money under
import java.util.Scanner;
import java.util.Scanner;
public class Discount
{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
int p,k;
double d=0,rp,np,tax;
String name;
System.out.println("Enter name of the customer");
name=in.next();
System.out.println("Enter your choice");
k=in.nextInt();
if(k==1)
{
System.out.println("Enter the amount of purchase");
p=in.nextInt();
if(p<=20000)
d=p*5.0/100.0;
if(p>20000 && p<=40000)
d=p*7.5/100.0;
if(p>40000 && p<=60000)
d=p*10.0/100.0;
if(p>60000)
d=p*12.0/100.0;
rp=p-d;
tax=rp*12.5/100.0;
np=rp+tax;
System.out.println("Customer's name:"+name);
System.out.println("The price of the product:Rs."+p);
System.out.println("The discount:Rs"+d);
System.out.println("The tax:Rs."+tax);
System.out.println("The amount to be paid:Rs."+np);
}
if(k==2)
{
System.out.println("enter the amount of purchase");
p=in.nextInt();
if(p<=20000)
d=p*2.5/100.0;
if (p>20000 && p<=40000)
d=p*5.0/100.0;
if (p>40000 && p<60000)
d=p*7.0/100.0;
if(p<=60000)
d=p*8.5/100.0;
rp=p-d;
tax=rp*12.5/100.0;
np=rp+tax;
System.out.println("Customer's name:"+name);
System.out.println("The price of the product:Rs."+p);
System.out.println("The tax:Rs."+tax);
System.out.println("the amount to be paid:Rs."+np);
}}}