Calculate Total Electricity Bill in Java


Write a program to input electricity unit charges and calculate total electricity bill according to the given condition:

  • For first 50 units Rs. 0.50/unit
  • For next 150 units Rs. 0.75/unit
  • For next 250 units Rs. 1.20/unit
  • For unit above 250 Rs. 1.50/unit
  • An additional surcharge of 20% is added to the bill

  • The program starts by importing the Scanner class from the java.util package, which allows the user to enter input from the console.
  • Next, the main method is defined, which is the entry point of the program. Inside the main method, a new Scanner object is created to read input from the console. Then, the user is prompted to enter the number of units consumed using the System.out.print statement. The input is read using the nextInt method of the Scanner class and stored in an integer variable unit.
  • Next, the program calculates the amount payable based on the number of units consumed using the following formula:
    • Amount Payable = Number of Units * Rate per Unit
  • The rate per unit is calculated based on the value of unit using a series of conditional statements (if-else). If unit is less than or equal to 50, the rate per unit is 0.50. If unit is between 51 and 150, the rate per unit is 0.75. If unit is between 151 and 250, the rate per unit is 1.20. If unit is greater than 250, the rate per unit is 1.50.
  • After calculating the amount payable, the program calculates the surcharge, which is 20% of the amount payable. The total electricity bill is the sum of the amount payable and the surcharge.
  • Finally, the total electricity bill is printed to the console using the System.out.println statement.

Source Code

import java.util.Scanner;
class Unit_Charges
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.print("Enter The Electricity Unit : ");
		int unit = input.nextInt();
		float amt,surcharge,bill_amt;
		if(unit<=50)
		{			
			amt = unit*0.50f;
		}
		else if(unit<=150)
		{
			amt = unit*0.75f;
		}
		else if(unit<=250)
		{
			amt = unit*1.20f;
		}
		else
		{
			amt = unit*1.50f;
		}
 
		surcharge = amt*0.2f;
		bill_amt = amt+surcharge;
		System.out.println("Total Electricity Bill : "+bill_amt);
	}
}

Output

Enter The Electricity Unit : 86
Total Electricity Bill : 77.4

Example Programs