Java Program to Calculate Electricity Bill

bookmark

import java.util.Scanner;
public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // prompt user to enter total units consumed
        System.out.print("Enter total units consumed: ");
        double totalUnitsConsumed = sc.nextDouble();
        //double variable 'cost' declared and initialized to 0
        //this will hold total bill price
        double cost = 0;
        // calculate bill amount
        if (totalUnitsConsumed < 50)
            cost = totalUnitsConsumed * 2.5;
        else if (totalUnitsConsumed < 100)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1;
        else if (totalUnitsConsumed < 250)
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7;
        else
            cost = 50 * 2.5 + (totalUnitsConsumed - 50) * 4.1 + (totalUnitsConsumed - 100) * 4.7
                    + (totalUnitsConsumed - 250) * 5.1;
        // display bill amount
        System.out.println("Bill amount is Rs." + cost);
    }
}

 


Output:

Enter total units consumed: 206
Bill amount is Rs.1262.8