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

If Else Programming

The document contains examples of Java programs that use conditional statements like if-else, switch-case to solve problems. The programs cover topics like finding the largest of two numbers, checking positive/negative/zero, even/odd checking, voter eligibility, sales commission calculation, day/month name from number, maximum of three numbers, divisibility, electricity bill calculation, leap year checking, triangle type identification and more. The programs are meant for teaching conditional programming concepts to students of ICSE/ISC/CBSE boards.

Uploaded by

Review Point
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
126 views

If Else Programming

The document contains examples of Java programs that use conditional statements like if-else, switch-case to solve problems. The programs cover topics like finding the largest of two numbers, checking positive/negative/zero, even/odd checking, voter eligibility, sales commission calculation, day/month name from number, maximum of three numbers, divisibility, electricity bill calculation, leap year checking, triangle type identification and more. The programs are meant for teaching conditional programming concepts to students of ICSE/ISC/CBSE boards.

Uploaded by

Review Point
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

PROGRAMMING

PROGRAMMING
JAVA
JAVA CLASSES
CLASSES
ICSE
ICSE AND
AND ISC
ISC
BOARD
BOARD
M:
M: 7017669878
7017669878
PROGRAMMERS POINT
Computer Application / Computer Science (For ICSE/ISC/CBSE Board)
Add: Madhav Kunj, Opp. SEA TV office, Behind Diamond Gym, Agra
Behind Kundkund Dharmshala, Opp. Collectorate Office, Agra
PEC Coaching centre, Sector- 4, Avas vikas Colony, Sikandra, Agra Mob: 827365168
Ankuram Education, Punchwati (Parasnath) Near Shankar Green, Fatehabad Road, 5
701795418
4
Class : X IF ELSE PROGRAMMING

1. WAP to print the larger of two numbers entered from the keyboard.

class i1
{
public static void main(int a,int b)
{
if(a>b)
System.out.println("Greatest number is "+a);
else
System.out.println("Greatest number is "+b);
}
}

2. WAP to accept a number from keyboard and check whether it is a positive number, negative
number or zero .print suitable message.

import java.util.*;
class i2
{
public static void main()//n=0
{
Scanner sc=new Scanner(System.in);
int n;
System.out.println("Enter any number");
n=sc.nextInt();

if(n>0)
System.out.println("positive number is "+n);
else if(n<0)
System.out.println("Negative number is "+n);
else if(n==0)
System.out.println("Number is Zero");
}
}

COMPUTER CODING CLASSES 7017669878


3. WAP a program to check a number to be Odd or even. Print suitable message for the number.

class i3
{
public static void main(int n)//n=7
{
if(n%2==0)
System.out.println("it is a Even number");
else
System.out.println("it is a Odd number");
}
}

4. A voter is eligible to vote the election held in his city if his age is at least 18 yrs. Define a class
Eligibility test that checks for the eligibility of a voter.
class i4
{
public static void main(int age)
{
if(age>=18)
System.out.println("Voter is eligible to vote");
else
System.out.println("voter is not eligible to vote");
}
}

5. Write a program that returns the commission in rupees for a salesman:


The commission is based on the following condition:
Sales Commission
<Rs 5000 5%
Rs 5000 to Rs 10000 8%
Above Rs 10000 10%

class i5
{
public static void main(int sales)
{
double comm=0.0;
if(sales<5000)
comm=sales*3.0/100.0;
else if(sales>=5000 && sales<=10000)
comm=sales*8.0/100.0;
else if(sales>10000)
comm=sales*10.0/100.0;
System.out.println("commission of salesman = "+comm);
}
}

COMPUTER CODING CLASSES 7017669878


6. Write a program to accept a day number from the user and print the corresponding day name.
make use of switch-case construct.

class i6
{
public static void main(int day)
{
switch(day)
{
case 1:
System.out.println("Monday");
break;
case 2:
System.out.println("Tuesday");
break;

case 3:
System.out.println("Wednesday");
break;

case 4:
System.out.println("Thursday");
break;

case 5:
System.out.println("Friday");
break;

case 6:
System.out.println("Saturday");
break;

case 7:
System.out.println("Sunday");
break;

default:
System.out.println("Wrong choice");
}
}
}

COMPUTER CODING CLASSES 7017669878


7. WAP to accept a month number and print the corresponding month name. make use of
switch-case construct.

class i7
{
public static void main(int month)
{
switch(month)
{
case 1:
System.out.println("jan");
break;

case 2:
System.out.println("feb");
break;

case 3:
System.out.println("march");
break;
.
.
.
case 12:
System.out.println("Dec");
default:
System.out.println("Wrong choice");
}
}
}

8. Define a java class maximum to print the largest out of the three numbers accepted from the user.

class i8
{
public static void main(int a,int b,int c)
{
if(a>b && a>c)
System.out.println("Greatest number is "+a);
else if(b>a && b>c)
System.out.println("Greatest number is "+b);
else if(c>a && c>b)
System.out.println("Greatest number is "+c);
}
}

COMPUTER CODING CLASSES 7017669878


9. WAP to check the divisibility of number a by another number say b.

class i9
{
public static void main(int a,int b)
{
if(a%b==0)
System.out.println(" a is divisible by b");
else
System.out.println(" a is not divisible by b");
}
}

10. An electricity board charges the bill depending on the number of units consumed as follows.
Units Charge
First 100 units 40 ps/per unit
Next 200 units 60 ps/per unit
Above 300 Rs 1 per unit

class i10
{
public static void main(int units)
{
double bill=0.0;
if(units<=100)
bill=units*.40;
else if(units>=101 && units<=300)
bill=100*.40+(units-100)*.60;
else if(units>300)
bill=100*.40+200*.60+(units-300)*1;
System.out.println("Electricity Bill = "+bill);
}
}
11. WAP class Leap to accept an year and check if it a leap year or not.
class i11
{
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
int year;
System.out.println("Enter any year");
year = sc.nextInt();
if (( year%400 == 0)|| (( year%4 == 0 ) &&( year%100 != 0)))
System.out.format(year +" is Leap year");
}
else
}
COMPUTER CODING CLASSES 7017669878
System.out.format(ye
ar +" is not Leap
year");

COMPUTER CODING CLASSES 7017669878


12. WAP to accept the length of three sides of a triangle and print appropriate message for the type
it belongs to e.g. Scalene, Equilateral or Isosceles.

import java.util.*;
class i12
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int A,B,C;
System.out.println("Enter first side of triangle");
A=sc.nextInt();
System.out.println("Enter second side of triangle");
B=sc.nextInt();
System.out.println("Enter third side of triangle");
C=sc.nextInt();
if(A==B && B==C && C==A)
System.out.println("Equilateral Triangle");
else if(A==B || B==C || C==A)
System.out.println("Isosceles Triangle");
else if(A!=B && B!=C && C!=A)
System.out.println("Scalene Triangle");
}
}

13. WAP to compute the amount that a customer pays for the taxi that he hires based on the following
condition:
Kms Travelled Amount per km
First 10 kms 25
Next 20 kms 10
Next 40 kms 15
Above 70 kms 12
Input the customer name, the taxi number and the number of kilometers travelled by him.

class i13
{
public static void main(String name,int taxino,int km)
{
int Fare=0;
if(km<=10)
Fare=km*25;
else if(km>10 && km<=30)
Fare=10*25+(km-10)*10;
else if(km>30 && km<70)
Fare=10*25+20*10+(km-30)*15;
else
Fare=10*25+20*10+40*15+(km-70)*12;
System.out.println("Fare of taxi = "+Fare);
COMPUTER CODING CLASSES 7017669878
}
}

COMPUTER CODING CLASSES 7017669878


14. WAP to compute the net amount paid by the customer.
Amount of purchase Mill cloth Handloom Item
Less than Rs 1000 2% 5%
Rs 1000 to Rs 5000 20% 25%
Above 5000 40% 50%

import java.util.*;
class i14
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int amount,choice;
System.out.println("Enter the purchasing amount");
amount=sc.nextInt();
System.out.println("Enter your choice");
System.out.println("case 1 for mill cloth ");
System.out.println("case 2 for Handloom item");
choice=sc.nextInt();
double dis=0.0;
switch(choice)
{
case 1:
if(amount<1000)
dis=amount*2.0/100.0;
else if(amount>=1000 && amount<=5000)
dis=amount*20.0/100.0;
else
dis=amount*40.0/100.0;
System.out.println("Discount on Mill Cloth = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;
case 2:
if(amount<1000)
dis=amount*5.0/100.0;
else if(amount>=1000 && amount<=5000)
dis=amount*25.0/100.0;
else
dis=amount*50.0/100.0;
System.out.println("Discount on Handloom Item = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;
default:
System.out.println("Wrong choice");

COMPUTER CODING CLASSES 7017669878


}
}
}

COMPUTER CODING CLASSES 7017669878


15. WAP to input 3 angles of a triangle check & print whether it is acute ,obtuse or right angle triangle.

class i15
{
public static void main(int A,int B,int C)
{
if(A<90 && B<90 && C<90)
System.out.println("Acute triangle");

else if(A>90 || B>90 || C>90)


System.out.println("Obtuse triangle");

else if(A==90 || B==90 || C==90)


Sytem.out.println("Right angle triangle");
}
}

16. WAP to print any no check & print whether it is BUZZ no or not.
it is divided by 7 and remainder is 7.

class i16
{
public static void main(int n)
{
if(n%7==0 || n%10==7)
System.out.println("Buzz number");
else
System.out.println("not a Buzz number");
}
}

17. WAP any no check & print whether it is Appropriate no or not.

class i17
{
public static void main(int n)
{
if(n%2==0 && n%3==0)
System.out.println("Appropriate number");
else
System.out.println("Not an Appropriate number");

COMPUTER CODING CLASSES 7017669878


}
}

COMPUTER CODING CLASSES 7017669878


18. WAP to compute the tax according to the given conditions and display the output.
Total Annual Taxable Income Tax Rate
Up to Rs 100000 No tax
From Rs. 100001 to Rs. 150000 10% of the income
Above 150000 30%

class i18
{
public static void main(int income)
{
double tax=0.0;
if(income<=100000)
tax=income*0.0/100.0;
else if(income>=100001 && income<=150000)
tax=income*10.0/100.0;
else if(income>150000)
tax=income*30.0/100.0;

System.out.println("tax of taxpayer = "+tax);


}
}

19. A cloth showroom has announced the following festival discount on the total cost of item purchased,
WAP to input the total cost and display the amount to be paid by the purchaser.
Total Cost Discount
upto 2000 15%
>2000 to 5000 25%
> 5000 to 10000 35%
>10000 50%

import java.util.*;
class i19
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int cost;
System.out.println("Enter the cost");
cost=sc.nextInt();

double dis=0.0;

COMPUTER CODING CLASSES 7017669878


if(cost<=2000)
dis=cost*15.0/100.0;
else if(cost>2000 && cost<=5000)

COMPUTER CODING CLASSES 7017669878


dis=cost*25.0/100.0;
else if(cost>5000 && cost<=10000)
dis=cost*35.0/100.0;
else if(cost>10000)
dis=cost*50.0/100.0;

System.out.println("Discount = "+dis);
}
}

20. WAP to input CP & SP of an article calculates & print its Profit% or loss% .if CP & SP are equal
then print no profit no loss.

class i20
{
public static void main(int sp,int cp)
{
int p,l;
double pp,lp;
if(sp>cp)
{
p=sp-cp;
pp=(p*100)/cp;
System.out.println("Profit percent = "+pp);
}
else
{
l=cp-sp;
lp=(l*100)/cp;
System.out.println("Loss percent = "+lp);
}
}
}

21. WAP to input total no of monthly calls, calculate & print the monthly tel. bill as given criteria.
No of Calls Call Rate
First 100 80 ps./call
Next 200 60 ps./call
Beyond 300 40 ps./call

class i21

COMPUTER CODING CLASSES 7017669878


{
public static void main(int calls)
{

COMPUTER CODING CLASSES 7017669878


double bill=0.0;
if(calls<=100)
bill=calls*.80;
else if(calls>=101 && calls<=300)
bill=100*.80+(calls-100)*.60;
else
bill=100*.80+200*.60+(calls-300)*.40;

System.out.println("Bil = "+bill);
}
}

22. Define a class to accept sales of a salesman and print his commission according to the following
criteria.
Sales(s) Commission
50000 onward s 15%
33000 <=s <50000 9%
18000 <=s <33000 6%
6000 <=s <18000 3%
0 <=s <6000 0%

import java.util.*;
class i22
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int sales;
double comm=0.0;
System.out.println("Enter the sales of salesman");
sales=sc.nextInt();
if(sales>=50000)
comm=sales*15.0/100.0;
else if(sales>=33000 && sales<50000)
comm=sales*9.0/100.0;
else if(sales>=18000 && sales<33000)
comm=sales*6.0/100.0;
else if(sales>=6000 && sales<18000)
comm=sales*3.0/100.0;
else if(sales>0 && sales<6000)
comm=0.0;

COMPUTER CODING CLASSES 7017669878


System.out.println("commission = "+comm);
}
}

COMPUTER CODING CLASSES 7017669878


23. To foster a sense of water conservation, the water department has an annual water conversion tax
policy. The rates are based on the water consumption of the consumer.
The tax rules are as follows:
Water consumed (Gallons) Tax rate (in Rs.)
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
class i23
{
public static void main(int gallons)
{
double tax=0.0;
if(gallons>45 && gallons<=75)
tax=475.0;
else if(gallons>75 && gallons<=125)
tax=750.0;
else if(gallons>125 && gallons<=200)
tax=1225.0;
else if(gallons>200 && gallons<=350)
tax=1650.0;
else if(gallons>350)
tax=2000.0;
System.out.println("Tax rate = "+tax);
}
}

24. An electricity board charges the following rates to domestic users to discourage large consumption of
electricity.
Units Charge
First 100 units 40 ps/per unit
Next 200 units 60 ps/per unit
Above 300 Rs 1 per unit

If the bill is above Rs. 1000 upto Rs. 2000 an extra surcharge of 10% is added. If the bill amount is above
Rs. 2000 a surcharge of 20% is added.
Write a program to enter the units consumed and print the bill in the proper format.

class i24
{
public static void main(int units)
{
COMPUTER CODING CLASSES 7017669878
double bill=0.0;
if(units<=100)
bill=units*.40;

COMPUTER CODING CLASSES 7017669878


else if(units>=101 && units<=300)
bill=100*.40+(units-100)*.60;
else
{
bill=100*.40+200*.60+(units-300)*1.0;
if(bill>1000 && bill<=2000)
bill=bill+bill*10.0/100.0;
else
bill=bill+bill*20.0/100.0;
}
System.out.println("Bil = "+bill);
}
}

25. An employee is entitled to pay an income tax based on his gross annual income as follows:
Annual gross income Annual tax deduction
Less than or equal to Rs. 100000 0%
100001 to 500000 Rs. 1000 + 10% of income exceeding Rs. 100000
500001 to 800000 Rs. 5000 + 20% of income exceeding Rs. 500000
> 800000 Rs. 10000 + 30% of income exceeding Rs. 800000
Write a program to accept the monthly salary of a person and print the annual tax paid by the salaried
person.

class i25
{
public static void main(int Msalary)
{
double tax=0.0;
int Asalary=Msalary*12;
if(Asalary<=100000)
tax=0.0;
else if(Asalary>=100001 && Asalary<=500000)
tax=1000+(Asalary-100000)*10.0/100.0;
else if(Asalary>=500001 && Asalary<=800000)
tax=5000+(Asalary-500000)*20.0/100.0;
else
tax=10000+(Asalary-800000)*30.0/100.0;
System.out.println("Tax of Employee = "+tax);
}
}

26. Write a program to enter item no., quantity purchased and rate. Calculate the total purchase price
and print it with the gifts. The to be the customers are given in the following manner:

Amount of Purchase(Rs) 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
COMPUTER CODING CLASSES 7017669878
class i26
{
public static void main(int no,int qty,int rate)
{
int p=qty*rate;
if(p>=100 && p<500)
System.out.println("a key ring");
else if(p>=500 && p<1000)
System.out.println("a leather purse");
else
System.out.println("a pocket calculator");
}
}

27. A library charges fine according to the number of a book is returned late, according to the following
criteria:
Days Fine per day
First 10 days Rs. 1
Next 10 days Rs. 1.50
Above 20 days Rs. 2.00
WAP to enter the number of days a book is returned late and print the fine to be paid.

class i27
{
public static void main(int days)
{
double fine=0.0;
if(days<=10)
fine=days*1;
else if(days>=11 && days<=20)
fine=10*1+(days-10)*1.50;
else if(days>=20)
fine=10*1+10*1.50+(days-20)*2.00;
System.out.println("Fine of student ="+fine);
}
}

28. A cloth showroom offers discounts in its regular stock clearance sale as per follows:
Amount of purchase Discount in %
Cloth material Readymade garments
Less than or equal to 500 3% 2%
Greater than 500 upto 2000 8% 5%
Greater than 2000 upto 5000 12% 10%
Greater than 5000 20% 15%

COMPUTER CODING CLASSES 7017669878


WAP to accept the amount of purchase and type of purchase where ‘R’ indicates readymade garments
and ‘C’ indicates cloth material either calculate discount and the discounted price for cloth material or
readymade garment depending on the type.

COMPUTER CODING CLASSES 7017669878


import java.util.*;
class i28
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int amount;
char choice;
System.out.println("Enter the purchasing amount");
amount=sc.nextInt();
System.out.println("Enter your choice");
System.out.println("case R for cloth material ");
System.out.println("case C for Readymade garments");
choice=sc.next().charAt(0);
double dis=0.0;
switch(choice)
{
case ‘R’:
if(amount<=500)
dis=amount*3.0/100.0;
else if(amount>500 && amount<=2000)
dis=amount*8.0/100.0;
else if(amount>2000 && amount<=5000)
dis=amount*12.0/100.0;
else
dis=amount*20.0/100.0;
System.out.println("Discount on Cloth Material = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;
case ‘C’:
if(amount<=500)
dis=amount*2.0/100.0;
else if(amount>500 && amount<=2000)
dis=amount*5.0/100.0;
else if(amount>2000 && amount<=5000)
dis=amount*10.0/100.0;
else
dis=amount*15.0/100.0;
System.out.println("Discount on Readymade Garments = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;
default:
System.out.println("Wrong choice");
}
}
}

COMPUTER CODING CLASSES 7017669878


29. A Computer shop has announced the following festivals discounts on purchase of items.
Amount of purchase Discount in %
Laptop Desktop
Less than or equal to 20000 3% 2%
Greater than 20000 upto 50000 8% 5%
Greater than 50000 upto 100000 12% 10%
Greater than 100000 20% 15%

Write a java program to compute the net amount paid by the customer. Assume all required
values to be inputted by the user. Make use of if and switch structure.

import java.util.*;
class i29
{
public static void main()
{
Scanner sc=new Scanner(System.in);
int amount,choice;
double dis=0.0;
System.out.println("Enter the purchasing amount");
amount=sc.nextInt();

System.out.println("Enter your choice");


System.out.println("case 1 for laptop");
System.out.println("case 2 for desktop");
choice=sc.nextInt();

switch(choice)
{
case 1:
if(amount<=20000)
dis=amount*3.0/100.0;
else if(amount>20000 && amount<=50000)
dis=amount*8.0/100.0;
else if(amount>50000 && amount<=100000)
dis=amount*12.0/100.0;
else
dis=amount*20.0/100.0;
System.out.println("Discount on Laptop = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;
COMPUTER CODING CLASSES 7017669878
case 2:
if(amount<=20000)
dis=amount*2.0/100.0;
else if(amount>20000 && amount<=50000)
dis=amount*5.0/100.0;
else if(amount>50000 && amount<=100000)
dis=amount*10.0/100.0;
else
dis=amount*15.0/100.0;
System.out.println("Discount on Desktop = "+dis);
System.out.println("Net amount to paid = "+(amount-dis));
break;

default:
System.out.println("Wrong choice");
}
}
}

30.Using a switch 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:
(Hint: C=5/9 *(F-32) and F=1.8*C+32)

import java.util.*;
class i30
{
public static void main()
{
Scanner sc=new Scanner(System.in);
double F,C;
int choice;
System.out.println("Enter your choice");
System.out.println("Case 1 for convert celcius to fahrenheit");
System.out.println("Case 2 for convert fahrenheit to celcius");
choice=sc.nextInt();
switch(choice)
{
case 1:
System.out.println("Enter the temp. in celcius");
C=sc.nextDouble();
F=1.8*(C+32);
COMPUTER CODING CLASSES 7017669878
System.out.println("Temp. in Fahrenheit =
"+F); break;

COMPUTER CODING CLASSES 7017669878


case 2:
System.out.println("Enter the temp. in fahrenheit");
F=sc.nextDouble();
C=(5./9.0)*(F-32);
System.out.println("Temp. in Celcius = "+C);
break;

default:
System.out.println("Wrong choice");
}
}
}

COMPUTER CODING CLASSES 7017669878

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy