On This page, all the programs are used if-else, if-else if-else branches. We are requesting all the visitors of this page, please do not copy the program. Just understand it and write the code in your own style. In Java, We can write one or more condition in if branch if the condition is TRUE then it execute all the statement(s) in if branch, but if the result FALSE then it execute all the statement(s) in else branch.
Accept a number from user and check it is even or odd.
import java.util.*;
public class EvenOdd
{
public static void main(String []args)
{
int no;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter A Number:");
no=sc.nextInt();
if(no%2==1)
{
System.out.print("Odd Number");
}
else
{
System.out.print("Even No");
}
}
}Output:
Enter A Number:12
Even No
Enter A Number:13
Odd Number
Accept a number from user and check it is magic or not.
import java.util.*;
public class Magic
{
public static void main(String []args)
{
int no;
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter A Number: ");
no=sc.nextInt();
if(no%9==1)
{
System.out.print("Magic Number");
}
else
{
System.out.print("Not A Magic Number");
}
}
}Output:
Enter A Number: 244
Magic Number
Enter A Number: 243
Not a Magic Number
An electronics shop has announced the following seasonal discounts on the purchase of certain items.
Purchase Amount in Rs.
Discount on Laptop
Discount on Desktop PC
0 – 25000
0.0%
5.0%
25001 – 57000
5.0%
7.6%
57001 – 100000
7.5%
10.0%
More than 100000
10.0%
15.0%
Write a program based on the above criteria to input name, address, amount of purchase and type of purchase (L for Laptop and D for Desktop) by a customer. Compute and print the net amount to be paid by a customer along with his name and address.[ICSE 2009]
(Hint: discount = (discount rate/100)* amount of purchase
import java.util.*;
public class Electro
{
private String cname,address;
char top;
private double aop,net;
public Electro()
{
cname="";
address="";
aop=0.0;
net=0.0;
}void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Customer Name : ");
cname=sc.nextLine();
System.out.print("Enter Customer Address : ");
address=sc.nextLine();
System.out.print("Enter type of Product : ");
top=sc.nextLine().charAt(0);
System.out.print("Enter amount of purchase : ");
aop=sc.nextDouble();
}void calculate()
{
switch(top)
{
case 'D':
case 'd': if(aop<=25000)
{
net = aop - (aop*5)/100;
}
else if(aop>25000 && aop<=57000)
{
net = aop - (aop*7.6)/100;
}
else if(aop>57000 && aop<=100000)
{
net = aop - (aop*10)/100;
}
else if(aop>100000)
{
net = aop - (aop*15)/100;
}
break;case 'L':
case 'l': if(aop<=25000)
{
net = aop - (aop*0)/100;
}
else if(aop>25000 && aop<=57000)
{
net = aop - (aop*5)/100;
}
else if(aop>57000 && aop<=100000)
{
net = aop - (aop*7.5)/100;
}
else if(aop>100000)
{
net = aop - (aop*10)/100;
}
break;
}}
void display()
{
System.out.print("\n\n Result");
System.out.print("\n ======");
System.out.print("\n Customer Name is " + cname);
System.out.print("\n Customer Address is " + address);
System.out.print("\n Payble Amount is " + net);
}public static void main(String []args)
{
Electro e=new Electro();
e.input();
e.calculate();
e.display();
}
}OUTPUT
=======Enter Customer Name : Alok Sen
Enter Customer Address : 123/4 Garia,kolkata-70047
Enter type of Product : L
Enter amount of purchase : 34500Result
======
Customer Name is Alok Sen
Customer Address is 123/4 Garia,kolkata-70047
Payble Amount is 32775.0
Shasha Travels Pvt. Ltd. gives the following discount to its customers:
Ticket amount Discount
Above Rs 70000 – 18%
Rs 55001 to Rs 70000 – 16%
Rs 35001 to Rs 55000 – 12%
Rs 25001 to Rs 35000 – 10%
less than Rs 25001 – 2%Write a program to input the name and ticket amount for the customer and calculate the discount amount and net amount to be paid. Display the output in the following format for each customer :
NO. Name Ticket charges Discount Net amount
1 - - - -[ICSE 2010](Assume that there are 15 customers, first customer is given the serial no (SlNo.) 1, next customer 2 … and so on.
import java.util.*;
public class ShashaDiscount
{
private String []cname;
private double []amt;
private double []dis;
private double []net;
public ShashaDiscount()
{
cname=new String[15];
amt=new double[15];
dis=new double[15];
net=new double[15];
for(int i=0;i<15;i++)
{
cname[i]="";
amt[i]=0;
dis[i]=0;
net[i]=0;
}
}
public void input()
{
Scanner sc=new Scanner(System.in);
for(int i=0;i<15;i++)
{
System.out.print("Enter Customer name : ");
cname[i]=sc.next();
System.out.print("Enter Ticket Amount : ");
amt[i]=sc.nextDouble();
}
}
public void calculate()
{
for(int i=0;i<15;i++)
{
if(amt[i]>70000)
{
dis[i]=amt[i]*18/100;
}
else if(amt[i]>55000 && amt[i]<=70000)
{
dis[i]=amt[i]*16/100;
}
else if(amt[i]>35000 && amt[i]<=55000)
{
dis[i]=amt[i]*12/100;
}
else if(amt[i]>25000 && amt[i]<=35000)
{
dis[i]=amt[i]*10/100;
}
else if(amt[i]<=25000)
{
dis[i]=amt[i]*2/100;
}
net[i]=amt[i]-dis[i];
}
}
public void display()
{
System.out.println("SL. NO. Name Ticket charges Discount Net amount");
for(int i=0;i<15;i++)
{
System.out.println((i+1) + " " + cname[i] + " " + amt[i] + " " + dis[i] + " " + net[i]);
}
}
public static void main(String []args)
{
ShashaDiscount sd=new ShashaDiscount();
sd.input();
sd.calculate();
sd.display();
}
}OUTPUT
=======Enter Customer name : Animesh
Enter Ticket Amount : 25000
Enter Customer name : Arka
Enter Ticket Amount : 27000
.
.
.
Enter Customer name : Kartik
Enter Ticket Amount : 65300
Enter Customer name : Kakali
Enter Ticket Amount : 20000
SL. NO. Name Ticket charges Discount Net amount
1 Animesh 25000.0 500.0 24500.0
2 Arka 27000.0 2700.0 24300.0
.
.
.
14 Kartik 65300.0 10448.0 54852.0
15 Kakali 20000.0 400.0 19600.0
Define a class called mobike with the following description:
Instance variables/data members:
int bno – to store the bike’s number
int phno – to store the phone number of the customer
String name – to store the name of the customer
int days – to store the number of days the bike is taken on rent
int charge – to calculate and store the rental chargeMember methods:
void input( ) – to input and store the detail of the customer.
void computer( ) – to compute the rental charge
The rent for a mobike is charged on the following basis.
First five days Rs 500 per day;
Next five days Rs 400 per day
Rest of the days Rs 200 per day
void display ( ) – to display the details in the following format:
Bike No. PhoneNo. No. of days Charge[ICSE 2011]
import java.util.*;
public class Mobike
{
private int bno;
private int phno;
private String name;
private int days;
private int charge;
public Mobike()
{
bno=0;
phno=0;
name="";
days=0;
charge=0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter Bike Number : ");
bno=sc.nextInt();
System.out.print("Enter Phone Number : ");
phno=sc.nextInt();
System.out.print("Enter Customer Name : ");
name=sc.next();
System.out.print("Enter Number Of Days for rent : ");
days=sc.nextInt();
}
void compute()
{
if(days<=5)
{
charge = days * 500;
}
else if(days>5 && days<=10)
{
charge = 5 * 500 + (days-5) * 400;
}
else if(days>10)
{
charge = 5 * 500 + 5 * 400 + (days-10) * 200;
}
}
void display()
{
System.out.println("Bike No. PhoneNo. No. of days Charge");
System.out.println(bno + " " + phno + " " + days + " " + charge);
}
public static void main(String []args)
{
Mobike mb=new Mobike();
mb.input();
mb.compute();
mb.display();
}
}OUTPUT
=======Enter Bike Number : 12345
Enter Phone Number : 24102364
Enter Customer Name : Alok
Enter Number Of Days for rent : 8
Bike No. PhoneNo. No. of days Charge
12345 24102364 8 3700
Given below is a hypothetical table showing rates of Income Tax for male citizens below the age of 65 years:
Taxable Income (TI) in
Income Tax in
Does not exceed 1,60,000
Nil
Is greater than 1,60,000 and less than or equal to 5,00,000
( TI – 1,60,000 ) * 10%
Is greater than 5,00,000 and less than or equal to 8,00,000
[ (TI - 5,00,000 ) *20% ] + 34,000
Is greater than 8,00,000
[ (TI - 8,00,000 ) *30% ] + 94,000
Write a program to input the age, gender (male or female) and Taxable Income of a person.If the age is more than 65 years or the gender is female, display “wrong category*. If the age is less than or equal to 65 years and the gender is male, compute and display the Income Tax payable as per the table given above.[ICSE 2012]
import java.util.*;
public class IncomeTax
{
private int age;
private String gender;
private double income,tax;
public IncomeTax()
{
age=0;
gender="";
income=0;
tax=0;
}
public void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("\nEnter Your Age : ");
age=sc.nextInt();
System.out.print("\nEnter Gender (male/female) : ");
gender=sc.next();
System.out.print("\nEnter Your Income : ");
income=sc.nextDouble();
}
public void calculate()
{
if(gender.toLowerCase().equals("female") || age>65)
{
System.out.println("Wrong Category");
}
else if(gender.toLowerCase().equals("male"))
{
if(income<=160000)
{
tax=0;
}
else if(income>160000 && income<=500000)
{
tax= (income-160000)*10/100;
}
else if(income>500000 && income<=800000)
{
tax= 34000 + (income-500000)*20/100;
}
else
{
tax= 94000 + (income-800000)*30/100;
}
}
else
{
System.out.println("Wrong Category");
}
}
public void display()
{
if(gender.toLowerCase().equals("male") && age<65)
{
System.out.print("Payble Tax is " + tax);
}
}
public static void main(String []args)
{
IncomeTax it=new IncomeTax();
it.input();
it.calculate();
it.display();
}
}OUTPUT
=======
Enter Your Age : 35Enter Gender (male/female) : male
Enter Your Income : 189000
Payble Tax is 2900.0
Enter Your Age : 67Enter Gender (male/female) : female
Enter Your Income : 567778
Wrong Category
Define a class named movieMagic with the following description:
Instance variables/data members:
int year – to store the year of release of a movie
String title – to store the title of the movie.
float rating – to store the popularity rating of the movie.
(minimum rating = 0.0 and maximum rating = 5.0)Member Methods:
movieMagic() Default constructor to initialize numeric data members to 0 and String data member to “”.
void accept() To input and store year, title and rating.
void display() To display the title of a movie and a message based on the rating as per the table below.
Rating
Message to be displayed
0.0 to 2.0
Flop
2.1 to 3.4
Semi-hit
3.5 to 4.5
Hit
4.6 to 5.0
Super Hit
Write a main method to create an object of the class and call the above member methods. [ICSE 2014]
import java.util.*;
public class movieMagic
{
private int year;
private String title;
private float rating;
public movieMagic()
{
year=0;
title="";
rating=0;
}
void accept()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Year : ");
year=sc.nextInt();
System.out.print("Enter The Title : ");
title=sc.next();
System.out.print("Enter The Rating : ");
rating=sc.nextFloat();
if (rating<0 || rating >5)
{
System.out.println("Invalid Rating");
rating=0;
}
}
void display()
{
System.out.println("Title of the movie is " + title);
if(rating>=0 && rating<=2)
{
System.out.println("Rating is Flop");
}
else if(rating>2 && rating<3.5)
{
System.out.println("Rating is Semi Hit");
}
else if(rating>=3.5 && rating<4.5)
{
System.out.println("Rating is Hit");
}
else if(rating>4.5 && rating<=5)
{
System.out.println("Rating is Super Hit");
}
}
public static void main(String []args)
{
movieMagic mm=new movieMagic();
mm.accept();
mm.display();
}
}OUTPUT
=======Enter The Year : 2017
Enter The Title : MOM
Enter The Rating : 3.6
Title of the movie is MOM
Rating is Hit
Define a class named BookFair with the following description:
String Bname – stores the name of the book.
double price – stores the price of the book.
Member Methods:
BookFair() Default constructor to initialize data members.
void Input() To input and store the name and the price of the book.
void calculate() To calculate the price after discount. Discount is calculated based on the following criteria.
Price
Discount
Less than or equal to Rs 1000
2% of price
More than Rs 1000 and less than or equal to Rs 3000
10% of price
More than Rs 3000
15% of price
void display() To display the name and price of the book after discount.
Write a main method to create an object of the class and call the above member methods.[ICSE 2016]
import java.util.*;
public class BookFair
{
private String Bname;
private double price;
public BookFair()
{
Bname="";
price=0.0;
}
void input()
{
Scanner sc=new Scanner(System.in);
System.out.print("Enter The Book Name : ");
Bname=sc.next();
System.out.print("Enter The Price of the Book : ");
price=sc.nextDouble();
}
void calculate()
{
if(price<=100)
{
price -= price*2/100;
}
else if(price >1000 && price<=3000)
{
price -= price*10/100;
}
else if(price >3000)
{
price -= price*15/100;
}
}
void display()
{
System.out.println("\n\nResult");
System.out.println("======");
System.out.println("Book Name is " + Bname + ".");
System.out.println("Price after discount is " + price);
}
public static void main(String []args)
{
BookFair bf=new BookFair();
bf.input();
bf.calculate();
bf.display();
}
}Output
Enter The Book Name : Networking
Enter The Price of the Book : 2500
Result
======
Book Name is Networking.
Price after discount is 2250.0