Electric Bill Java Program

Electric Bill Java Program | Program description:- Write a Java program to calculate the electricity bill for the given unit.

Before, writing the Java program, first, let us know about the tariff rates. The fixed price is 100, and then if we consume electricity between 0-100 unit then we have to pay 6.30 per unit, if we consume electricity between 100-200 unit then for the first 100 we have to pay 6.30 per unit (i.e. 100*6.30), and for remaining pay 6.85 per unit. Similarly, if we consume >200 units then we have to pay 6.30 per unit for the first 100 units, 6.85 per unit for the next 100 units, and 7.40 per unit for the remaining units.

Tarrif rates (urban, demanded),

Fixed PriceUnitCharges per unit
1000-1006.30
100101-2006.85
100>2007.40

For example:-

Unit = 270
Total bill = fixed price + bill for first 100 unit (0-100) + bill for next 100 unit (101-200) + bill for remaiing unit (>200)
= 100 + 1006.30 + 1006.85 + (270-200)*7.40
= 100 + 630 + 685 + 518
= 1933

Electric Bill Java Program

import java.util.Scanner;
public class ElectricBill {
  public static void main(String[] args) {

    // create Scanner class object
    Scanner scan = new Scanner(System.in);

    // read input for unit
    System.out.print("Enter unit: ");
    int unit = scan.nextInt();

    // calculate electric bill
    double bill = calculateElectricBill(unit);

    // display result
    System.out.println("Bill amount = " + bill);
  }

  // Java method to calculate the electric bill amount
  public static double calculateElectricBill(int unit) {

    // tariff rates
    double fixed_price = 100;
    double rate0_100 = 6.30;
    double rate101_200 = 6.85;
    double rateMoreThan200 = 7.40;

    // calculate bill
    if(unit <= 100) {
      return fixed_price + (unit * rate0_100);
    }
    else if(unit <= 200) {
      return ( fixed_price + 
               (100 * rate0_100) + 
               (unit-100)*rate101_200 
             );
    }
    else {
      return ( fixed_price + 
              (100 * rate0_100) + 
               (100 * rate101_200) + 
               (unit-200)* rateMoreThan200 
             );
    }
  }
}

Output for the different test-cases:-

Enter unit: 270
Bill amount = 1933.0

Enter unit: 40
Bill amount = 352.0

Enter unit: 150
Bill amount = 1072.5

To solve the problem, we have used a nested if-else statement in the program. The nested if-else is a multi-decision statement. When we have to choose one option among the multiple options then we are using a nested if-else statement. To check the option we have to write conditions, which is unit-based. If the unit is between 1-100 then the “if” block will execute, and if the unit is 101-200 then the “else if” block will execute, otherwise the “else” block will execute.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also see:- Simple Calculator Program, BMI (Body Mass Index) Calculator, Check Even number in Java

Leave a Reply