img

Mastering If-Else Statements: 8 Solved Practice Questions

This is the solution for the practice question set on conditional (if else) statements. You can find the list of questions here. Try solving the questions yourself before you go through the solutions.


Electricity Billing System

 

Problem: Write a program to calculate the electricity bill based on the following rates:

     - Tier 1: First 100 units – ₹5 per unit

     - Tier 2: Next 100 units (101-200) – ₹7 per unit

     - Tier 3: Above 200 units – ₹10 per unit


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        double units, ebill=0.0;

        //taking the number of units as input

        System.out.print("Enter no. of units : ");

        units=sc.nextDouble();

        //performing calculations

        if(units<=100)

        {

            ebill=units*5;

        }

        else if(units>100 && units<=200)

        {

            ebill=500+(units-100)*7;

        }

        else

        {

            ebill=500+700+(units-200)*10;

        }

        //printing ebill as output

        System.out.println("Electricity Bill : "+ebill);

    }

}


Library Late Fee Calculation

  

Problem: A library charges a late fee based on the number of days a book is overdue:

     - 1-5 days: ₹5 per day

     - 6-10 days: ₹10 per day

     - More than 10 days: ₹20 per day


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int days, lfee=0;

        //taking the number of overdue days as input

        System.out.print("Enter no. of days : ");

        days=sc.nextInt();

        //performing calculations

        if(days<=5)

        {

            lfee=days*5;

        }

        else if(days>5 && days<=10)

        {

            lfee=25+(days-5)*10;

        }

        else

        {

            lfee=25+50+(days-10)*20;

        }

        //printing lfee as output

        System.out.println("Library Late Fee : "+lfee);

    }

}


Income Tax Calculation

 

Problem: Write a program that calculates income tax based on the following slabs:

     - Below ₹2.5 lakh: No tax

     - ₹2.5 lakh to ₹5 lakh: 5% tax

     - ₹5 lakh to ₹10 lakh: 20% tax

     - Above ₹10 lakh: 30% tax


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        double salary, tax=0.0;

        //taking the salary amount as input

        System.out.print("Enter the salary : ");

        salary=sc.nextDouble();

        //performing calculations

        if(salary<=250000)

        {

            tax=0.0;

        }

        else if(salary>250000 && salary<=500000)

        {

            tax=0.05*salary;

        }

        else if(salary>500000 && salary<=1000000)

        {

            tax=0.2*salary;

        }

        else

        {

            tax=0.3*salary;

        }

        //printing tax as output

        System.out.println("Tax amount on salary : "+tax);

    }

}


Telecom Bill Calculation


Problem: A telecom company charges for internet usage as follows:

     - Up to 10 GB: ₹100 per GB

     - 11-20 GB: ₹80 per GB

     - Above 20 GB: ₹50 per GB

   - Condition: If the customer has a “premium” membership, apply a 15% discount to the total bill. Calculate the final bill considering the membership status.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        double usage, tbill=0.0;

        char ch;

        //taking the internet usage as input

        System.out.print("Enter the internet usage in GB : ");

        usage=sc.nextDouble();

        //performing calculations

        if(usage<=10)

        {

            tbill=usage*100;

        }

        else if(usage>10 && usage<=20)

        {

            tbill=1000+(usage-10)*80;

        }

        else

        {

            tbill=1000+800+(usage-20)*50;

        }

        //confirming the premium membership

        System.out.println("Does the customer have a premium membership? ");

        System.out.print("Y/N ? ");

        ch=sc.next().charAt(0);

        if(ch=='Y')

        {

            tbill=tbill-(0.15*tbill);

        }

        //printing tax as output

        System.out.println("Final bill considering membership status : "+tbill);

    }

}


Grade Classification with Bonus Points

 

Problem: In a university, grades are calculated based on a score out of 100:

     - 90-100: A

     - 80-89: B

     - 70-79: C

     - Below 70: Fail

   - Condition: If the student has participated in extracurricular activities, add 5 bonus points to their score before grading. Write a program that determines the final grade with these conditions.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        double score;

        char ch;

        String grade="";

        //taking the score as input

        System.out.print("Enter the score : ");

        score=sc.nextDouble();

        //confirming extracurricular participation

        System.out.println("Did the student participate in extracurriculars? ");

        System.out.print("Y/N ? ");

        ch=sc.next().charAt(0);

        if(ch=='Y')

        {

            score=score+5;

        }

        //performing calculations

        if(score>=90 && score<=100)

        {

            grade="A";

        }

        else if(score>=80 && score<=89)

        {

            grade="B";

        }

        else if(score>=70 && score<=79)

        {

            grade="C";

        }

        else if(score<70)

        {

            grade="Fail";

        }

        //printing final grade

        System.out.println("Final grade : "+grade);

    }

}


Hospital Bill Calculation Based on Room Type


Problem: A hospital charges for rooms as follows:

     - General Ward: ₹1,000 per day

     - Semi-Private: ₹2,000 per day

     - Private Room: ₹3,500 per day

   - Condition: If a patient is a senior citizen, apply a 10% discount on the total bill. Calculate the final bill amount for the patient based on the number of days and room type.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int room,days;

        double tbill=0.0;

        char ch;

        //taking the room type as input

        System.out.println("1 for General-Ward");

        System.out.println("2 for Semi-Private");

        System.out.println("3 for Private Room");

        System.out.print("Enter the room type number : ");

        room=sc.nextInt();

        //taking the number of days as input

        System.out.print("Enter the no. of days : ");

        days=sc.nextInt();

        //performing calculations

        if(room==1)

        {

            tbill=days*1000;

        }

        else if(room==2)

        {

            tbill=days*2000;

        }

        else if(room==3)

        {

            tbill=days*3500;

        }

        else

        {

            System.out.println("Invalid Room Type!");

        }

        //confirming senior citizenship

        System.out.println("Is the patient more than 60 years old? ");

        System.out.print("Y/N ? ");

        ch=sc.next().charAt(0);

        if(ch=='Y')

        {

            tbill=tbill-(0.10*tbill);

        }

        //printing tax as output

        System.out.println("Final bill considering age : "+tbill);

    }

}


Online Store Discount Calculation


Problem: An online store offers discounts based on the purchase amount:

     - Below ₹1,000: No discount

     - ₹1,000-₹5,000: 10% discount

     - ₹5,000-₹10,000: 15% discount

     - Above ₹10,000: 20% discount

   - Condition: For “VIP” customers, apply an additional 5% discount on the final amount after the initial discount. Write a program to calculate the final amount for both regular and VIP customers.


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        double purchase,fbill=0.0;

        char ch;

        //taking the purchase amount as input

        System.out.print("Enter the purchase amount : ");

        purchase=sc.nextDouble();

        //performing calculations

        if(purchase<=1000)

        {

            fbill=purchase;

        }

        else if(purchase>1000 && purchase<=5000)

        {

            fbill=purchase-(0.10*purchase);

        }

        else if(purchase>5000 && purchase<=10000)

        {

            fbill=purchase-(0.15*purchase);

        }

        else

        {

            fbill=purchase-(0.2*purchase);

        }

        //confirming VIP membership

        System.out.println("Is the customer a VIP member? ");

        System.out.print("Y/N ? ");

        ch=sc.next().charAt(0);

        if(ch=='Y')

        {

            fbill=fbill-(0.05*fbill);

        }

        //printing final bill as output

        System.out.println("Final bill considering membership : "+fbill);

    }

}


Leap Year


Problem: Write a program that takes a year as input and prints whether it is a leap year or not (i.e., divisible by 4, but not 100, unless divisible by 400).


CODE : 


import java.io.*;

import java.util.*;


class Main

{

    public static void main(String[] args)

    {

        Scanner sc = new Scanner(System.in);

        //declaring all variables

        int year;

        //taking the year as input

        System.out.print("Enter the year : ");

        year=sc.nextInt();

        //performing calculations

        if(year%4==0 && year%100!=0 || year%400==0)

        {

            System.out.println("A Leap Year!");

        }

        else

        {

            System.out.println("Not a Leap Year!");

        }


    }

}

post-img

Numbers In Java : 10 Practice Problems a...

This is the solution for the practice question set on different numbers (like Harshad, Armstrong, Perfect, and so on).

Read More

post-img

For While Loops: 10 Practice Problems an...

This is the solution for the practice question set on the concept of iteration statements (for and while loops).

Read More

post-img

Switch Case Simplified: 5 Solved Practic...

This is the solution for the practice question set on the concepts of switch case and fall through.

Read More

philomathiq

© 2023 philomathiq. All rights reserved.