You are on page 1of 23

ICSE COMPUTER APPLICATION

SOLVED PROGRAMS
CHAPTER: CONDITIONAL STATEMENT

Program1.Write a program to input 2 numbers and check which is the greatest


between two numbers.

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);
}}}

Program 3. Write a program to enter three numbers and a character.


Find and display sum of the numbers if the given character is ’s’ and product of
the numbers if the given character is 'p'. The program displays a message "Invalid
Character" if the user enters a letter other than 's' or 'p'.
import java.util.*;
public class Numbers
{
public static void main (String args[])
{Scanner sc=new Scanner(System.in);
int a,b,c,sum=0,pr=0;
char ch;
System.out.println("Enter three numbers");
a=sc.nextInt();
b=sc.nextInt();
c=sc.nextInt();
System.out.println("Enter 's' for sum and 'p' for product of three numbers");
ch=sc.next().charAt(0);
if(ch=='s')
{
sum=a+b+c;
System.out.println("The sum of three numbers:"+sum);
}
else if (ch=='p')
{
pr=a*b*c;
System.out.println("The product of three numbers:"+pr);
}
else
System.out.println("Entered an invalid character!!!");
}}
Program 4. Write a program to accept a mark obtained by a student in
computer science and print the grades accordingly:
Marks Grade
Above 90 A
70 to 89 B
50 to 69 C
below 50 D

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;

public class TriangleAngle1


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first angle: ");
int a1 = in.nextInt();
System.out.print("Enter second angle: ");
int a2 = in.nextInt();
System.out.print("Enter third angle: ");
int a3 = in.nextInt();
int Sum = a1 + a2 + a3;
if (Sum == 180 && a1 > 0 && a2 > 0 && a3 > 0) {
if (a1 < 90 && a2 < 90 && a3 < 90) {
System.out.println("Acute-angled Triangle");
}
else if (a1 == 90 || a2 == 90 || a3 == 90) {
System.out.println("Right-angled Triangle");
}
else if(a1>90&&a1<180||a2>90&&a2<180||a3>90&&a3<180)
{
System.out.println("Obtuse-angled Triangle");
}
}
else {
System.out.println("Triangle not possible");
}
}
}

Program 7. Write a program to input year and check whether it is:


(a) a Leap year (b) a Century Leap year (c) a Century year but not a Leap year
Century years are NOT leap years UNLESS they can be evenly divided by 400.
(For example, 1700, 1800, and 1900 were not leap years, but 1600 and 2000,
which are divisible by 400, were.)
Sample Input: 2000
Sample Output: It is a Century Leap Year.

import java.util.Scanner;

public class LeapYear


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter the year to check: ");
int yr = in.nextInt();

if (yr % 4 == 0 && yr % 100 != 0)


System.out.println("It is a Leap Year");
else if (yr % 400 == 0)
System.out.println("It is a Century Leap Year");
else if (yr % 100 == 0)
System.out.println("It is a Century Year but not a Leap Year");
else
System.out.println("It is neither a Century Year nor a Leap Year");
}
}

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;

public class Divisor


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter number: ");
int num = in.nextInt();

if (num % 3 == 0 && num % 5 == 0)


System.out.println("Divisible by 3 and 5");
else if (num % 3 == 0)
System.out.println("Divisible by 3 but not by 5");
else if (num % 5 == 0)
System.out.println("Divisible by 5 but not by 3");
else
System.out.println("Neither divisible by 3 nor by 5");
}
}

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:

Total Cost Discount Gift


Up to Rs. 2,000 5% Calculator
Rs 2,001 to Rs. 5,000 10% School Bag
Rs.5,001 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.

import java.util.Scanner

public class Cloth_Discount


{
public static void main(String args[])
{
Scanner in = new Scanner(System.in);
System.out.print("Enter total cost: ");
double cost = in.nextDouble();
String gift;
double amt;

if (cost <= 2000.0)


{
amt = cost - (cost * 5.0 / 100.0);
gift = "Calculator";
}
else if (cost <= 5000.0)
{
amt = cost - (cost * 10.0 / 100.0);
gift = "School Bag";
}
else if (cost <= 10000.0) {
amt = cost - (cost * 15.0 / 100.0);
gift = "Wall Clock";
}
else if(cost>10000) {
amt = cost - (cost * 20.0 / 100.0);
gift = "Wrist Watch";
}
System.out.println("Amount to be paid: " + amt);//printing statement of
amount
System.out.println("Gift: " + gift);//printing the gift item

}
}

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;

public class Perf_Square


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter first number: ");
int a = in.nextInt();
System.out.print("Enter second number: ");
int b = in.nextInt();

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);
}
}

Program 15. A courier company charges differently for “ORDINARY” Booking


and
“EXPRESS” Booking based on the weight of the parcel as per the tariff given up

Weight Ordinary Booking Express booking


up to 100 gm Rs.80 Rs.100
101 gm to 500 gm Rs.150 Rs.200
501 gm to 1000 gm Rs.210 Rs.250
more than 1000 gm Rs.250 Rs.300
Write a program to input weight of a parcel and type of
booking (‘O’ for ordinary and ’E ‘ for Express ). Calculate and print the charges
accordingly.

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.

Taxable income (TI) in Rs. Income Tax in Rs.


Up to Rs. 2,50,000 Nil
More than Rs. 2,50,000 and less than or
equal to Rs. 5,00,000 (TI - 1,60,000) * 10%
More than Rs. 5,00,000 and less than or
equal to Rs. 10,00,000 (TI - 5,00,000) * 20% + 34,000
More than Rs. 10,00,000 (TI - 10,00,000) * 30% + 94,000

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;

public class IncomeTax_11


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter age: ");
int age = in.nextInt();
System.out.print("Enter taxable income: ");
double ti = in.nextDouble();
double tax = 0.0;

if (age > 60) {


System.out.print("Wrong Category");
}
else {
if (ti <= 250000)
tax = 0;
else if (ti <= 500000)
tax = (ti - 160000) * 10.0 / 100.0;
else if (ti <= 1000000)
tax = 34000 + ((ti - 500000) * 20.0 / 100.0);
else
tax = 94000 + ((ti - 1000000) * 30 / 100);
}

System.out.println("Name: " + name);


System.out.println("Tax Payable: " + tax);
}
}

Program 17. 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 ₹ 10,000 10% 5%
₹ 10,001 - ₹ 20,000 12% 8%
₹ 20,001 - ₹ 30,000 15% 10%
₹ 30,001 and above 20% 12%

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;

public class SalaryBasic


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter name: ");
String name = in.nextLine();
System.out.print("Enter basic salary: ");
double bs = in.nextDouble();
double da = 0.0, sa = 0.0;

if (bs <= 10000){


da = bs * 10.0 / 100.0;
sa = bs * 5.0 / 100.0;
}
else if (bs <= 20000){
da = bs * 12.0 / 100.0;
sa = bs * 8.0 / 100.0;
}
else if (bs <= 30000){
da = bs * 15.0 / 100.0;
sa = bs * 10.0 / 100.0;
}
else{
da = bs * 20.0 / 100.0;
sa = bs * 12.0 / 100.0;
}

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.

Sum Assured Discount Commission


Up to Rs. 1,00,000 5% 2%
Rs. 1,00,001 and up to Rs. 2,00,000 8% 3%
Rs. 2,00,001 and up to Rs. 5,00,000 10% 5%
More than Rs. 5,00,000 15% 7.5%

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;

public class Lic_premium


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter Name: ");
String name = in.nextLine();
System.out.print("Enter Sum Assured: ");
double sum = in.nextDouble();
System.out.print("Enter First Premium: ");
double pre = in.nextDouble();
double disc = 0.0, comm = 0.0;

if(sum <= 100000){


disc = pre * 5.0 / 100.0;
comm = sum * 2.0 / 100.0;
}
else if(sum >=100001&& sum<= 200000){
disc = pre * 8.0 / 100.0;
comm = sum * 3.0 / 100.0;
}
else if(sum <= 500000){
disc = pre * 10.0 / 100.0;
comm = sum * 5.0 / 100.0;
}
else if (sum>500000){
disc = pre * 15.0 / 100.0;
comm = sum * 7.5 / 100.0;
}

System.out.println("Name of the policy holder: " + name);


System.out.println("Sum assured: " + sum);
System.out.println("Premium: " + pre);
System.out.println("Discount on the first premium: " + disc);
System.out.println("Commission of the agent: " + comm);

}
}
Program 19. An employee wants to deposit certain sum of money under

“Term Deposit” scheme in Syndicate Bank.


The bank has provided the tariff of the scheme which is given below:

NO. OF DAYS RATE OF INTERES NO. OF DAYS RATE OF INTEREST


Up to 180 days 5.5% Exact 365 days 9.0%
181 to 364 days 7.5% More than 365 days 8.5%

import java.util.Scanner;

public class Term_Depo


{
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
System.out.print("Enter sum of money: ");
double sum = in.nextDouble();
System.out.print("Enter number of days: ");
int days = in.nextInt();
double interest = 0.0; double amt=0.0;

if (days <= 180)


interest = sum * 5.5 / 100.0;
else if (days <= 364)
interest = sum * 7.5 / 100.0;
else if (days == 365)
interest = sum * 9.0 / 100.0;
else
interest = sum * 8.5 / 100.0;

amt = sum + interest;

System.out.print("Maturity Amount = " + amt);


}
}

Program 20 Electronic world' has announced an off-Season discount on the


purchase of certain items as per the given bellow
Amount Discount on A/C Discount on LCD TV
up to Rs. 20,000 5% 2.5%
Rs. 20,001 - Rs. 40,000 7.5% 5%
Rs. 40,001 - Rs. 60,000 10% 7%
more than Rs. 60,000 12% 8.5%

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);
}}}

You might also like