Loading your workspace…
Loading your workspace…
Read a customer's name and units consumed, apply the slab tariff, add the meter charge and the 15% surcharge above Rs 400, and print the bill.
The syllabus says all users are charged 'a minimum of Rs. 100 as meter charge'. This program treats it as a fixed Rs 100 added to every bill, which is how the reference outputs in the college lab manuals are computed. Confirm the reading with your lab instructor if your manual differs.
#include <stdio.h>
#define METER_CHARGE 100.0
#define SURCHARGE_LIMIT 400.0
#define SURCHARGE_RATE 0.15
double energy_charge(int units) {
double charge;
if (units <= 200) {
charge = units * 0.80;
} else if (units <= 300) {
charge = 200 * 0.80 + (units - 200) * 0.90;
} else {
charge = 200 * 0.80 + 100 * 0.90 + (units - 300) * 1.00;
}
return charge;
}
int main(void) {
char name[50];
int units;
double bill;
printf("Enter customer name:\n");
scanf("%49s", name);
printf("Enter units consumed:\n");
if (scanf("%d", &units) != 1 || units < 0) {
printf("Error: units must be a non-negative whole number\n");
return 1;
}
bill = METER_CHARGE + energy_charge(units);
if (bill > SURCHARGE_LIMIT) {
bill = bill + bill * SURCHARGE_RATE;
}
printf("Customer : %s\n", name);
printf("Units : %d\n", units);
printf("Bill : Rs %.2f\n", bill);
return 0;
}
Input
Ramesh
457Output
Enter customer name:
Enter units consumed:
Customer : Ramesh
Units : 457
Bill : Rs 583.05Input
Shyam
0Output
Enter customer name:
Enter units consumed:
Customer : Shyam
Units : 0
Bill : Rs 100.00The tariff is progressive: each slab is priced only on the units that fall inside it. The energy_charge function therefore adds the cost of the first 200 units, the next 100 units and the remainder, instead of pricing all units at one rate.
The surcharge is applied to the whole bill and only when the bill exceeds Rs 400, so the check happens after the meter charge is added. The constants are defined with #define so the rates are easy to change in one place.
Edit the program and press Run. It is free and needs no sign-in.
Copy a sample and paste it into the STDIN box in the editor before you press Run.
Ramesh 457
Expected output
Enter customer name: Enter units consumed: Customer : Ramesh Units : 457 Bill : Rs 583.05
Shyam 0
Expected output
Enter customer name: Enter units consumed: Customer : Shyam Units : 0 Bill : Rs 100.00
Keeping the tariff in energy_charge separates the slab logic from input and output, so the rule can be tested with any unit value.
The slab conditions use <= 200 and <= 300, so 300 is the last unit of the second slab and only units above 300 use the third rate.
The rates are decimal fractions such as 0.80 and 0.90, so a floating-point type keeps the paise values instead of truncating them.
It limits the number of characters stored in name so that a long input cannot overflow the 50-byte array.
The rule compares the total bill with Rs 400, and the meter charge is part of that total.
Back to all VTU BPOPS103 lab programs.