Skip to content

Lab · C Programming (from zero to systems)

Lab: slab-wise electricity bills

Labbeginner· about 1.5 hours· 4 steps

The brief

Indian electricity boards charge by slabs: the first units are cheap and each slab above costs more. You write the slab rule in JavaScript first and test it here, then write the same logic in C with if/else, bill a whole list of meter readings in a loop, and print an average with a cast.

Why it matters

Slab tariffs are everywhere: electricity, water, income tax and mobile data plans. Billing software and the firmware inside smart meters (written in C) apply exactly this logic, and a slab bill is a favourite question in campus placement coding rounds because the boundaries (100 or 101 units) catch careless code.

Skills you practise

  • if and else if
  • while loop with scanf
  • continue
  • integer division and casts
  • printf with %.2f

You need

  • gcc (Linux, macOS with Xcode command line tools, or Windows with MSYS2 or WSL)

What you will have at the end

slabs.js with a tested energyCharge(), and bill.c: a C program that bills a list of meter readings slab by slab, skips bad readings and prints a summary with the average.

How it is marked (rubric, 100 points)
Correct billsEvery check passes, including the 100 and 101 unit boundaries and the skipped negative reading.45
Right control flowSlabs with if statements, a while loop that stops at the end of input, and continue for bad readings.25
Types and castsMoney in double, units in int, and a cast before the division for the average.15
Clean buildNo warnings with -Wall -Wextra, and no prompts mixed into the output.15

0/4 steps done

Step 1 of 4

The slab rule, tested in JavaScript

energyCharge(units) returns the energy charge in rupees: 1050 for 250 units, 304.5 for 101 units.

Work out the logic first in JavaScript, where it is tested for you; the C version in the next step uses the same if statements. Start from the top slab: if units is above 300, charge the units above 300 at 7.50 and keep only 300 for the slabs below. Then do the same for 200 and 100, and charge whatever is left at 3.00.

A common mistake is to charge all the units at the rate of the highest slab reached (101 units × 4.50). That is not how slab tariffs work: only the 1 unit above 100 costs 4.50.

Run shows what your code prints. Check runs the step's tests. Ctrl+Enter checks too.