Electricity Bill: flowchart, algorithm and program

This program reads the number of units used and calculates the electricity bill using slab rates: 1.5 per unit for the first 100, 2.5 for the next 100, and 4.0 for everything above 200. It uses an if / else if / else chain to find which slab the units fall in, then adds up the cost of each slab.

▶ Watch the dry run step by step →

Algorithm

  1. Step 1: Start.
  2. Step 2: Read units.
  3. Step 3: If units <= 100, go to step 4; otherwise go to step 5.
  4. Step 4: Set bill = units * 1.5. Go to step 8.
  5. Step 5: If units <= 200, go to step 6; otherwise go to step 7.
  6. Step 6: Set bill = 100*1.5 + (units-100)*2.5. Go to step 8.
  7. Step 7: Set bill = 100*1.5 + 100*2.5 + (units-200)*4.0.
  8. Step 8: Print bill.
  9. Step 9: Stop.

Pseudocode

BEGIN
    READ units
    IF units <= 100 THEN
        bill ← units * 1.5
    ELSE
        IF units <= 200 THEN
            bill ← 100*1.5 + (units-100)*2.5
        ELSE
            bill ← 100*1.5 + 100*2.5 + (units-200)*4.0
        END IF
    END IF
    PRINT bill
END

Program in C, Python and Java

Electricity Bill program in C

#include <stdio.h>

int main() {
    int units;
    float bill;

    printf("Enter units used: ");
    scanf("%d", &units);

    if (units <= 100) {
        bill = units * 1.5;
    } else if (units <= 200) {
        bill = 100 * 1.5 + (units - 100) * 2.5;
    } else {
        bill = 100 * 1.5 + 100 * 2.5 + (units - 200) * 4.0;
    }

    printf("Total bill = %.2f\n", bill);
    return 0;
}

Electricity Bill program in Python

units = int(input("Enter units used: "))

if units <= 100:
    bill = units * 1.5
elif units <= 200:
    bill = 100 * 1.5 + (units - 100) * 2.5
else:
    bill = 100 * 1.5 + 100 * 2.5 + (units - 200) * 4.0

print("Total bill = %.2f" % bill)

Electricity Bill program in Java

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int units;
        double bill;

        System.out.print("Enter units used: ");
        units = sc.nextInt();

        if (units <= 100) {
            bill = units * 1.5;
        } else if (units <= 200) {
            bill = 100 * 1.5 + (units - 100) * 2.5;
        } else {
            bill = 100 * 1.5 + 100 * 2.5 + (units - 200) * 4.0;
        }

        System.out.printf("Total bill = %.2f\n", bill);
    }
}

Sample input: units = 250
Output: Total bill = 600.00

How it works

  • Read units with scanf and declare bill as a float so the result can have decimals.
  • If units <= 100, every unit costs 1.5, so bill = units * 1.5.
  • If units <= 200, the first 100 units cost 100 * 1.5 and only the extra (units - 100) are charged at 2.5.
  • Otherwise the first two slabs are full: 100 * 1.5 + 100 * 2.5, and only (units - 200) are charged at 4.0.
  • For units = 250: 150 + 250 + 50 * 4.0 = 600, printed with %.2f as 600.00.

Dry run (trace table)

StepLineBoxunitsbillOutput
13Start
28Read units250Enter units used:
310units <= 100?250
412units <= 200?250
515bill = 100*1.5 + 100*2.5 + (units-200)*4.0250600.0
618Print bill250600.0Total bill = 600.00
719Stop250600.0

Time and space complexity

Time: O(1) · Space: O(1). Only a fixed number of comparisons and calculations are done, regardless of the input value.

Common mistakes

  • Charging all units at the rate of the highest slab (for example units * 4.0) instead of charging each slab at its own rate.
  • Forgetting to subtract the earlier slabs, writing units * 2.5 instead of (units - 100) * 2.5.
  • Using < instead of <= in the conditions, so exactly 100 or 200 units fall into the wrong slab.

Viva questions

Q1. Why do you use an if / else if / else ladder here instead of three separate if statements?

The slabs are mutually exclusive, so only one branch should run. With else-if, once units <= 100 is true the other conditions are skipped, and the later conditions can safely assume the earlier ones failed.

Q2. In the second branch, why do you write 100 * 1.5 + (units - 100) * 2.5 and not just units * 2.5?

Slab billing is incremental: the first 100 units are charged at 1.5 and only the units above 100 are charged at 2.5. Using units * 2.5 would charge all units at the higher rate.

Q3. Where does the 100 * 1.5 + 100 * 2.5 in the third branch come from?

It is the fixed cost of the first two slabs: 150 for the first 100 units and 250 for the next 100 units. Only units above 200 are charged at 4.0, so we add (units - 200) * 4.0.

Q4. What is the bill for 0 units, for exactly 100 units and for exactly 200 units?

0 units gives 0.00, 100 units gives 150.00 and 200 units gives 400.00. Because of the <= conditions, 100 and 200 fall in the lower slab and both formulas agree at the boundary, so the bill is continuous.

Q5. What happens if the user enters a negative number of units? How would you handle it?

The program does not check it: units <= 100 is true, so it prints a negative bill. I would add a check such as if (units < 0) that prints an error message and exits.

Q6. The rates like 1.5 are doubles but bill is a float. Is that a problem, and why do we print with %.2f?

The calculation is done in double and then stored in a float, which can lose some precision, though it is fine for small bills. I would use double for money-like values. %.2f prints the bill rounded to two decimal places.

Q7. What is the time complexity of this program?

It is O(1), constant time. There are no loops, and only one condition chain and one calculation run, whatever the value of units.

Q8. What would change if the slab rates or limits changed, or if there were many slabs?

For a few changes I would only edit the constants in the formulas. With many slabs, I would store the limits and rates in arrays and use a loop to accumulate the cost slab by slab, and I would use #define or const variables instead of hard-coded numbers.

Frequently asked questions

How is the electricity bill calculated in this program?

The units are split into slabs: the first 100 at 1.5, the next 100 at 2.5, and the rest at 4.0. The cost of each slab is added to get the total bill.

What is the bill for 250 units?

100 * 1.5 = 150, 100 * 2.5 = 250 and 50 * 4.0 = 200, so the total is 600.00.

Why is bill declared as float and not int?

The rates 1.5 and 2.5 have decimal parts, so the bill can be fractional. A float (printed with %.2f) keeps the decimals, while an int would drop them.

More programs

← All programs

Try AI Flowchart free →