Electricity Bill Calculator
Calculate electricity bill based on units consumed using slab rates.
IntermediateTopic: Module 2: Conditional Programs
Java Electricity Bill Calculator Program
This program helps you to learn the fundamental structure and syntax of Java programming.
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("Enter units consumed: ");
int units = sc.nextInt();
double bill;
if (units <= 100) {
bill = units * 1.5;
} else if (units <= 200) {
bill = 100 * 1.5 + (units - 100) * 2.0;
} else {
bill = 100 * 1.5 + 100 * 2.0 + (units - 200) * 3.0;
}
System.out.println("Total bill = " + bill);
sc.close();
}
}Output
Enter units consumed: 250 Total bill = 500.0
Understanding Electricity Bill Calculator
We apply different per-unit rates depending on the slab the consumption falls into.
Note: To write and run Java programs, you need to set up the local environment on your computer. Refer to the complete article Setting up Java Development Environment. If you do not want to set up the local environment on your computer, you can also use online IDE to write and run your Java programs.