C PROGRAMMING PROGRAM • DECISION MAKING
Calculate an Electricity Bill
Learn how to calculate an electricity bill using a clear C program.
PROBLEM UNDERSTANDING
Input and expected output
Sample input
250
Sample output
Bill amount = 600.00
COMPLETE C PROGRAM
Complete C implementation
#include <stdio.h>
int main(void)
{
double units, bill;
printf("Enter consumed units: ");
scanf("%lf", &units);
if (units < 0) {
printf("Invalid units\n");
return 0;
}
if (units <= 100)
bill = units * 1.50;
else if (units <= 200)
bill = 100 * 1.50 + (units - 100) * 2.50;
else
bill = 100 * 1.50 + 100 * 2.50 + (units - 200) * 4.00;
printf("Bill amount = %.2f\n", bill);
return 0;
}CURRENT STEP
SELECTED LINE
EXPECTED OUTPUT FOR THE SAMPLE
Bill amount = 600.00
Step 0 of 0
PROGRAM EXPLANATION
Algorithm and explanation
- Read the required input values.
- Accumulate the cost of each completed slab and charge only the remaining units at the current rate.
- Display the computed result.
Accumulate the cost of each completed slab and charge only the remaining units at the current rate.
EFFICIENCY
Time and space complexity
Time complexity
O(1)
Auxiliary space
O(1)
DEBUGGING CHECKLIST
Common mistakes
Check this
Use the correct format specifier for every variable.
Check this
Initialize variables before using their values.
Check this
Check braces, semicolons and input order carefully.
Try it yourself
Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.
