An electric power distribution company charges its domestic consumers as follows:

Computation Units                         Rate of Charge

0-200                                                     Rs. 0.50 per unit

201-400                                                Rs. 100 plus Rs. 0.65 per unit excess of 200

401-600                                                Rs. 230 plus Rs. 0.80 per unit excess of 400

601 and above                                   Rs. 390 plus Rs. 1.00 per unit excess of 600

The program reads the customer number and power consumed and prints the amount to be paid by the customer.

Below is a Java program that reads the customer number and power consumed, then calculates and prints the amount to be paid by the customer based on the given rate structure.

				
					import java.util.Scanner;

public class ElectricityBillCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);

        // Requesting input from the user
        System.out.print("Enter customer number: ");
        int customerNumber = scanner.nextInt();
        System.out.print("Enter units consumed: ");
        int unitsConsumed = scanner.nextInt();

        double amount = 0.0;

        // Compute the amount based on the rate structure
        if (unitsConsumed <= 200) {
            amount = unitsConsumed * 0.50;
        } else if (unitsConsumed <= 400) {
            amount = 100 + (unitsConsumed - 200) * 0.65;
        } else if (unitsConsumed <= 600) {
            amount = 230 + (unitsConsumed - 400) * 0.80;
        } else {
            amount = 390 + (unitsConsumed - 600) * 1.00;
        }

        // Output the results
        System.out.println("Customer Number: " + customerNumber);
        System.out.println("Units Consumed: " + unitsConsumed);
        System.out.println("Amount to be paid: Rs. " + String.format("%.2f", amount));
    }
}

				
			

Explanation:

  1. Variable Declaration:

    • customerNumber: stores the customer number.
    • unitsConsumed: stores the number of units consumed.
    • amount: stores the calculated amount to be paid.
  2. Input Request:

    • The program requests the customer number and units consumed from the user.
  3. Computation:

    • The program calculates the amount based on the given rate structure using a series of if-else statements:
      • If units consumed are less than or equal to 200, the charge is unitsConsumed * 0.50.
      • If units consumed are between 201 and 400, the charge is 100 + (unitsConsumed - 200) * 0.65.
      • If units consumed are between 401 and 600, the charge is 230 + (unitsConsumed - 400) * 0.80.
      • If units consumed are above 600, the charge is 390 + (unitsConsumed - 600) * 1.00.
  4. Output Results:

    • The program prints the customer number, units consumed, and the amount to be paid.

Output:

Here are some example outputs of the program for different inputs:

  1. Example 1:

				
					Enter customer number: 101
Enter units consumed: 150
Customer Number: 101
Units Consumed: 150
Amount to be paid: Rs. 75.00

				
			

Example 2:

				
					Enter customer number: 102
Enter units consumed: 350
Customer Number: 102
Units Consumed: 350
Amount to be paid: Rs. 197.50

				
			

Example 3:

				
					Enter customer number: 103
Enter units consumed: 500
Customer Number: 103
Units Consumed: 500
Amount to be paid: Rs. 310.00


				
			

Example 4:

				
					Enter customer number: 104
Enter units consumed: 750
Customer Number: 104
Units Consumed: 750
Amount to be paid: Rs. 540.00



				
			

To compile and run this program, you can use a Java compiler:

  1. Save the code in a file named ElectricityBillCalculator.java.
  2. Open a terminal and navigate to the directory containing the file.
  3. Compile the program using the javac compiler:
				
					javac ElectricityBillCalculator.java




				
			

Run the compiled program using the java interpreter:

				
					java ElectricityBillCalculator




				
			

You should see the output displayed as shown above.

Scroll to Top