C Programs Tutorials | IT Developer
IT Developer

Java Programs



Share with a Friend

Operators & Expressions

Java Program: Electricity Consumption Bill Using Slabs

Slab System Used

Units Range

Rate per Unit

0 – 100 units

₹5

101 – 200 units

₹7

201 – 300 units

₹10

Above 300 units

₹15

 

import java.util.Scanner;

 

public class ElectricityBill {

    public static double calculateBill(int units) {

        double bill = 0;

 

        if (units <= 100) {

            bill = units * 5;

        } else if (units <= 200) {

            bill = (100 * 5) + (units - 100) * 7;

        } else if (units <= 300) {

            bill = (100 * 5) + (100 * 7) + (units - 200) * 10;

        } else {

            bill = (100 * 5) + (100 * 7) + (100 * 10) + (units - 300) * 15;

        }

 

        return bill;

    }

 

    public static void main(String[] args) {

        Scanner sc = new Scanner(System.in);

 

        System.out.print("Enter units consumed: ");

        int units = sc.nextInt();

 

        double amount = calculateBill(units);

 

        System.out.println("\n------- Electricity Bill -------");

        System.out.println("Units Consumed: " + units);

        System.out.printf("Total Amount: ₹%.2f\n", amount);

 

        sc.close();

    }

}

Output

INPUT :
Enter units consumed: 275
 
OUTPUT :
------- Electricity Bill -------
Units Consumed: 275
Total Amount: ₹1950.00

Explanation

Let’s understand how the slab is applied for 275 units:

  1. First 100 units (0 – 100) → ₹5 per unit

                  100 x 5 = ₹500

  1. Next 100 units (101 – 200) → ₹7 per unit

                 100 x 7 = ₹700

  1. Remaining 75 units (201 – 275) → ₹10 per unit

                  75 x 10 = ₹750 

Final Bill Calculation

                     Total = 500 + 700 + 750 = ₹1950