10 Basic Programs using if-else and switch case (Java)

So today I am going to give you some of the programs using if-else and switch case. If you know the basics of the if-else and switch case or if you have followed our earlier blogs then you will be able to write the codes which I am going to give. The programs which I am going to give, first you try to do it by yourself and then you can match the solution which I have provided. So without any further delay lets get started.

Q1. Write a program to accept two integers. Find and print the greatest from them otherwise print equal if both are same.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
 class GreatestNumber
 {
     public void main(int a,int b)
     {
         if(a > b)
         {
             System.out.println(a + " is the greatest number.");
         }
         else if(b > a)
         {
             System.out.println(b + " is the greatest number.");
         }
         else
         {
             System.out.println("Both the numbers are equal.");
         }
     }
 }

Q2. Write a program to accept three integers. Find and print the greatest and smallest from them.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 class GreatestAndSmallest
 {
     public void main(int a,int b,int c)
     {
         if(a > b && a > c)
         {
             System.out.println("The greatest value is A = " + a);
         }
         else if(b > a && b > c)
         {
             System.out.println("The greatest value is B = " + b);
         }
         else if(c > a && c > b)
         {
             System.out.println("The greatest value is C = " + c);
         }
         if(a < b && a < c)
         {
             System.out.println("The smallest value is A = " + a);
         }
         else if(b < a && b < c)
         {
             System.out.println("The smallest value is B = " + b);
         }
         else if(c < a && c < b)
         {
             System.out.println("The smallest value is C = " + c);
         }
     }
 }

Q3. An institution has decided to admit new candidates in different streams on the following criteria:

Total marks obtained                    Stream offered
300 and above                                Science
200 and above but less 
than 300                                        Commerce
Below 200 but not 
below 75                                          Arts
Otherwise                                       You are in the waiting list

Write a program to input total marks obtained in an examination and print the stream allotted.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
 class Stream
 {
     public void main(int totalmarks)
     {
         if(totalmarks >= 300)
         {
             System.out.println("Eligible for Science Stream");
         }
         else if(totalmarks < 300 && totalmarks >= 200)
         {
             System.out.println("Eligible for Commerce Stream");
         }
         else if(totalmarks < 200 && totalmarks >= 75)
         {
             System.out.println("Eligible for Arts Stream");
         }
         else
         {
             System.out.println("You are in the waiting list");
         }
     }
 }

Q4. A cloth showroom has announced the following festival discounts on the purchase of items, based on the total cost of the items purchased.

Total Cost                  Discount( in Percentage)
Less than Rs.2000                    5%
Rs.2001 to  Rs.5000                25%
Rs.5001 to Rs.10000               35%
Above Rs.10000                      50%

Write a program to define a method void PrintBill(double totalCost) to input the total cost. Computes the discount and the net amount to be paid by the customer after availing the discount. Print all the data.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 class ClothShowroom
 {
     public void printBill(double totalCost)
     {
         double netAmount = 0.0,dis = 0.0;
         if(totalCost <= 2000)
         {
             dis = (totalCost*5)/100;
         }
         else if(totalCost >= 2001 && totalCost <= 5000)
         {
             dis = (totalCost*25)/100;
         }
         else if(totalCost >= 5001 && totalCost <= 10000)
         {
             dis = (totalCost*35)/100;
         }
         else if(totalCost > 10000)
         {
             dis = (totalCost*50)/100;
         }
         netAmount = totalCost-dis;
         System.out.println("Total purchase amount = " + totalCost);
         System.out.println("Discount given = " + dis);
         System.out.println("Amount to be paid by customer = " + netAmount);
     }
 }

Q5. The telephone department computes monthly telephone bill using the given rules on the basis of calls:

Number of calls                   Rate
First 100                    Rs.250/- as rental charge
Next 100                    60 paisa per call + rental charge
Next 100                    50 paisa per call + rental
Any call above 300
calls                           40 paisa per call + rental charge

Write a program to input number of calls and compute total bill amount. Print all the data members.

Solution:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
 class TelephoneBill
 {
     public void main(int calls)
     {
         double amount = 0.0;
         if(calls <= 100)
         {
             amount = calls*250;
         }
         else if(calls > 100 && calls <= 200)
         {
             amount = 250+(calls-100)*0.60;
         }
         else if(calls < 200 && calls <= 300)
         {
             amount = 250+(100*0.60)+(calls-200)*0.50;
         }
         else if(calls > 300)
         {
             amount = 250+(100*0.60)+(100*0.50)+(calls-300)*0.40;
         }
         System.out.println("Total number of calls: " + calls);
         System.out.println("Total bill amount: " + amount);
     }
 }

Q6. Write a program to input product (pc) in character, quantity of the product purchased (qty) and rate of one piece of product (rate). Calculate the total purchase amount and print it along with the gift to be presented on the following basis:

Amount of purchase (Rs.)                Gift to be presented
200 and above but less than 700      A key chain
700 and above but less than 1200    A carry bag
1200 and above                                An electronic calculator

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 class PurchaseAndGetGift
 {
     public void main(int pc,int qty,double rate)
     {
         System.out.println("Product Code: " + pc);
         System.out.println("Quantity of product purchased: " + qty);
         System.out.println("Rate of one piece of product: " + rate);
         double totalAmount = qty*rate;
         System.out.println("Total Purchase Amount: " + totalAmount);
         if(totalAmount >= 200 && totalAmount < 700)
         {
             System.out.println("You received a gift: A Key Chain");
         }
         else if(totalAmount >= 700 && totalAmount < 1200)
         {
             System.out.println("You received a gift: A carry bag");
         }
         else if(totalAmount >= 1200)
         {
             System.out.println("You received a gift: An electronic calculator");
         }
     }
 }

Q7. Monthly electricity bill is calculated as per the given slabs:

Number of units consumed        Rate per unit
<= 200                                        only meter rent Rs.500/-
for next 200 units                        Rs. 1.00 per unit + meter rent
for next 200 unit                          Rs. 1.55 per unit + meter rent
for more than 500 units                Rs. 2.10 per unit + meter rent

Write a program to input consumer number(cn), number of units consumed(units). Calculate bill amount. Print consumer number and total amount to be paid by the consumer.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 class ElectricityBill
 {
     public void main(int cn,int units)
     {
         System.out.println("Consumer number: " + cn);
         System.out.println("Number of units consumed: " + units);
         double totalAmount = 0;
         if(units <= 200)
         {
             totalAmount = 500;
         }
         else if(units > 200 && units <= 400)
         {
             totalAmount = 500+(units-200)*1.00;
         }
         else if(units > 400 && units <= 600)
         {
             totalAmount = 500+(200*1.00)+(units-400)*1.55;
         }
         else if(units > 600)
         {
             totalAmount = 500+(200*1.00)+(200*1.55)+(units-600)*2.10;
         }
         System.out.println("Total amount to be paid by the consumer: " + totalAmount);
     }
 }

Q8. Write a program using switch case to compute the area of a:

(1) Area of Circle (PI*r2) where PI = 3.14
(2) Area of Square (side*side)
(3) Area of rectangle (length*breadth)

Display the menu to output the area as per User's choice.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 import java.util.*;
 class Area
 {
     public void main()
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Choose a option from below: ");
         System.out.println("Choose 1 for Area of Circle");
         System.out.println("Choose 2 for Area of Square");
         System.out.println("Choose 3 for Area of Rectangle");
         System.out.println("Make your choice : ");
         int a = sc.nextInt();
         double area;
         switch(a)
         {
             case 1:
             System.out.println("Enter radius of circle: ");
             int radius = sc.nextInt();
             area = 3.14*radius*radius;
             System.out.println("Area of Circle: " + area);
             break;
             case 2:
             System.out.println("Enter side of square: ");
             int side = sc.nextInt();
             area = side*side;
             System.out.println("Area of square: " + area);
             break;
             case 3:
             System.out.println("Enter length of rectangle: ");
             int length = sc.nextInt();
             System.out.println("Enter breadth of rectangle: ");
             int breadth = sc.nextInt();
             area = length*breadth;
             System.out.println("Area of rectangle: " + area);
             break;
             default:
             System.out.println("Invalid Choice");
         }
     }
 }

Q9. Write a program to input a digit (0-9) and using switch-case print the number name (if input is 4, the output is FOUR). The program should display an appropriate message if the digit entered is wrong.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
 import java.util.*;
 class NumberName
 {
     public void main()
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a digit between 0 to 9 : ");
         int a = sc.nextInt();
         switch(a)
         {
             case 0:
             System.out.println("ZERO");
             break;
             case 1:
             System.out.println("ONE");
             break;
             case 2:
             System.out.println("TWO");
             break;
             case 3:
             System.out.println("THREE");
             break;
             case 4:
             System.out.println("FOUR");
             break;
             case 5:
             System.out.println("FIVE");
             break;
             case 6:
             System.out.println("SIX");
             break;
             case 7:
             System.out.println("SEVEN");
             break;
             case 8:
             System.out.println("EIGHT");
             break;
             case 9:
             System.out.println("NINE");
             break;
             default:
             System.out.println("INVALID NUMBER");
         }
     }
 }

Q10. Write a program to input a number between 1 to 12. Using suitable switch-case print the name of the month using the input (if input is 6, output is JUNE and if input is 10, output OCTOBER). The program should display an appropriate message if the input number is wrong.

Solution:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
 import java.util.*;
 class MonthName
 {
     public void main()
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("Enter a digit between 1 to 12: ");
         int a = sc.nextInt();
         switch(a)
         {
             case 1:
             System.out.println("JANUARY");
             break;
             case 2:
             System.out.println("FEBRUARY");
             break;
             case 3:
             System.out.println("MARCH");
             break;
             case 4:
             System.out.println("APRIL");
             break;
             case 5:
             System.out.println("MAY");
             break;
             case 6:
             System.out.println("JUNE");
             break;
             case 7:
             System.out.println("JULY");
             break;
             case 8:
             System.out.println("AUGUST");
             break;
             case 9:
             System.out.println("SEPTEMBER");
             break;
             case 10:
             System.out.println("OCTOBER");
             break;
             case 11:
             System.out.println("NOVEMBER");
             break;
             case 12:
             System.out.println("DECEMBER");
             break;
             default:
             System.out.println("INVALID NUMBER");
         }
     }
 }

So that is it for the if-else and switch case programs. Hope you find it useful and if you have any query do comment in the comment box and I will try to solve your query.

Thank You.
10 Basic Programs using if-else and switch case (Java) 10 Basic Programs using if-else and switch case (Java) Reviewed by Get2Know on September 05, 2020 Rating: 5

No comments:

Business

Powered by Blogger.