C program to generate an electricity bill

In C, generating an electricity bill involves calculating charges based on different rate slabs depending on units consumed. Higher consumption leads to higher per-unit rates, implementing a progressive billing system commonly used by utility companies.

Syntax

if (units < slab1_limit) {
    amount = units * rate1;
    fixed_charge = charge1;
} else if (units <= slab2_limit) {
    amount = slab1_cost + ((units - slab1_limit) * rate2);
    fixed_charge = charge2;
}

Rate Structure

The electricity billing follows these rate slabs −

Units Consumed Rate per Unit Fixed Charge
0 - 50 ?3.50 ?25
51 - 100 ?4.25 ?35
101 - 200 ?5.26 ?45
Above 200 ?7.75 ?55

Example

Following is the C program to generate an electricity bill based on progressive rate slabs −

#include <stdio.h>

int main() {
    int units;
    float amt, unitcharg, total;
    
    printf("Enter no of units consumed: ");
    scanf("%d", &units);
    
    if (units < 50) {
        amt = units * 3.50;
        unitcharg = 25;
    } else if (units <= 100) {
        amt = 130 + ((units - 50) * 4.25);
        unitcharg = 35;
    } else if (units <= 200) {
        amt = 130 + 162.50 + ((units - 100) * 5.26);
        unitcharg = 45;
    } else {
        amt = 130 + 162.50 + 526 + ((units - 200) * 7.75);
        unitcharg = 55;
    }
    
    total = amt + unitcharg;
    
    printf("Units consumed: %d<br>", units);
    printf("Energy charges: %.2f<br>", amt);
    printf("Fixed charges: %.2f<br>", unitcharg);
    printf("Total electricity bill: %.2f<br>", total);
    
    return 0;
}
Enter no of units consumed: 280
Units consumed: 280
Energy charges: 1438.50
Fixed charges: 55.00
Total electricity bill: 1493.50

How It Works

The program uses cumulative pricing where each slab builds upon the previous one. For 280 units, the calculation is −

  • First 50 units: 50 × 3.50 = ?175.00
  • Next 50 units: 50 × 4.25 = ?212.50
  • Next 100 units: 100 × 5.26 = ?526.00
  • Remaining 80 units: 80 × 7.75 = ?620.00
  • Fixed charge: ?55.00
  • Total: ?1,588.50 (Note: The constants 130, 162.50, 526 represent cumulative costs of lower slabs)

Conclusion

This electricity billing program demonstrates progressive rate calculation using conditional statements in C. The system encourages energy conservation by charging higher rates for increased consumption.

Updated on: 2026-03-15T13:49:47+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements