Week 6: Slab Billing
An electricity bill and a telephone bill charged in slabs with a fixed charge and a percentage surcharge: the one else-if ladder every tariff reduces to, cumulative slab constants, and reading a customer's name with fgets before any scanf.
Tariffs in Slabs
Two bills, one shape. An electricity board and a telephone department both charge in slabs: the first block of units at one rate, the next block at a higher rate, everything beyond at the highest. Both add a fixed charge that every customer pays, and both add a percentage surcharge once the total crosses a threshold. The programs are the longest so far and they are also the most mechanical, because every slab tariff ever set is the same if, else if, else ladder from week 5 with the lower slabs' totals folded into constants. Learn the shape once and both programs, and every variant an exam can invent, fall out of it.
Money is held in double and printed with %.2f. The second program also reads a customer's name, which is the first time this chapter needs a string, so the chapter 8 line reader makes an appearance.
Program 1: An Electricity Bill
Read a meter number and the units consumed. The first 200 units cost 80 paise each, the next 100 cost 90 paise each, and every unit beyond 300 costs one rupee. Every customer pays a meter charge of Rs 100. If the total comes to more than Rs 400, a surcharge of 15% of the total is added.
Algorithm:
- Start.
- Read the meter number and the units consumed.
- If the units are negative, print a message and stop.
- If units ≤ 200, energy = units × 0.80; otherwise if units ≤ 300, energy = 160 + (units − 200) × 0.90; otherwise energy = 250 + (units − 300) × 1.00.
- total = energy + 100.
- If total > 400, surcharge = 15% of total; otherwise surcharge = 0. Add the surcharge to the total.
- Print the meter number, the units, the energy charge, the meter charge, the surcharge, and the total.
- Stop.
Run it with 1234 250: the first 200 units cost 160, the next 50 cost 45, the meter charge brings it to 305, and no surcharge applies. Then 1001 500: 160 plus 90 plus 200 is 450, plus 100 is 550, which is over 400, so 15% of 550, which is 82.50, goes on top for 632.50.
The ladder is the whole program. The middle branch is not units * 0.90; it is the full first slab, 200 * 0.80, plus the part of the consumption that falls in the second slab at the second rate. The third branch likewise carries the first two slabs in full. Writing the lower slabs as 200 * 0.80 rather than 160 costs nothing at run time, since the compiler folds the constant, and it leaves the tariff readable in the code, which is what a marker checks first. The commonest wrong answer charges all 250 units at 90 paise; the second commonest applies the surcharge to the energy charge alone, when the tariff says "of total amount", meter charge included.
One subtlety the lab manual skips. total > 400.0 compares a double against a threshold, and chapter 3 warned that arithmetic on decimals like 0.80 is approximate. It works here because each slab total happens to round to an exact value, and %.2f would hide a stray 0.0000000001 in the printout anyway, but a bank would not accept that argument: real billing code keeps money in paise as integers so that 80 × 200 is exactly 16000 and the threshold test is exact. The exercise accepts the double version, since it is what the syllabus wants; the sentence about paise is worth having ready.
Program 2: A Telephone Bill
Read the customer's name and the units used. The first 100 units cost 95 paise each, the next 75 cost Rs 1.50 each, and every unit beyond 175 costs Rs 2.85. Every customer pays a service charge of Rs 75. If the total exceeds Rs 500, a surcharge of 12.5% of the total is added.
Algorithm:
- Start.
- Read the customer's name as a whole line, then the units used.
- If the units are negative, print a message and stop.
- If units ≤ 100, call = units × 0.95; otherwise if units ≤ 175, call = 95 + (units − 100) × 1.50; otherwise call = 207.50 + (units − 175) × 2.85.
- total = call + 75.
- If total > 500, surcharge = 12.5% of total; otherwise surcharge = 0. Add the surcharge to the total.
- Print the name, the units, the call charge, the service charge, the surcharge, and the total.
- Stop.
Type a name on the first line, Asha Rao, and 200 on the second. The three slabs give 95, 112.50, and 71.25, a call charge of 278.75, and with the service charge 353.75, under the threshold. Try Ravi and 400: the call charge is 848.75, the total 923.75 crosses 500, and the 12.5% surcharge of 115.47 takes it to 1039.22.
The name is why this program reads differently from every earlier one. A name has a space in it, and scanf("%s") stops at the first space, so Asha Rao would come back as Asha with Rao left in the stream to wreck the next read. The chapter 8 answer is fgets, which reads the whole line into a bounded buffer, and the scan-and-replace loop that follows removes the newline it keeps. Reading the name first with fgets and the number second with scanf is the order that works; the reverse, scanf then fgets, leaves a newline in the stream that the fgets swallows as an empty line, the mixed-reader trap chapter 8 warned about.
Everything else is program 1 with different numbers, which is the lesson. The ladder has the same three branches with the same cumulative structure, the fixed charge is a symbolic constant, the surcharge is a second if on the total. When an exam paper describes a tariff you have never seen, this is the program to write.
Key Takeaways
- A slab tariff is an
if,else if,elseladder on the units, and each branch carries the full cost of every lower slab plus the part in its own slab. - Write the lower slabs as
200 * 0.80, not160, so the tariff stays visible; the compiler folds the constant. - The surcharge is a separate decision on the total after the fixed charge, and it applies to that whole total.
- Money in
doubleprinted with%.2fis what the syllabus wants; money in integer paise is what a bank wants, because then every threshold test is exact. - A name with spaces needs
fgetsand a newline trim, and it must be read before anyscanf, never after one.
How did you find this lesson?
Your rating helps us improve the content.
Create an account to track your progress and access interactive exercises. Already have one? Sign in.
Practice Exercises
An Electricity Bill in Slabs
Read a meter number and the number of units consumed, both integers, and print the bill. The first 200 units cost 80 paise each, the next 100 units cost 90 paise each, and every unit beyond 300 costs Rs 1.00. Every customer pays a meter charge of Rs 100. If the total amount, energy charge plus meter charge, is more than Rs 400, add a surcharge of 15% of that total. Print six lines in exactly this form: meter number: 1234, units consumed: 250, energy charge: 205.00, meter charge: 100.00, surcharge: 0.00, total amount: 305.00, with every amount to two decimal places. If the units are negative print units cannot be negative and return 1. If the two integers cannot be read print invalid input and return 1.
A Telephone Bill in Slabs
Read the customer's name as a whole line, which may contain spaces, then the number of units used as an integer on the next line, and print the bill. The first 100 units cost 95 paise each, the next 75 units cost Rs 1.50 each, and every unit beyond 175 costs Rs 2.85. Every customer pays a service charge of Rs 75. If the total amount, call charge plus service charge, is more than Rs 500, add a surcharge of 12.5% of that total. Print six lines in exactly this form: name: Asha Rao, units consumed: 200, call charge: 278.75, service charge: 75.00, surcharge: 0.00, total amount: 353.75, with every amount to two decimal places. Read the name with fgets and remove the trailing newline. If the units are negative print units cannot be negative and return 1. If the name or the units cannot be read print invalid input and return 1.
Lesson Discussion
Share your thoughts and questions
No comments yet. Be the first to share your thoughts!