Electricity Bill Calculator in C Program
In this tutorial, we are going to create an electricity bill calculator. This program is designed to help you calculate your electricity bill based on the units consumed. Let's see how it works.
Beginner Tip: If you are practicing foundational C programs, you can also explore our Simple POS System in C and Contact Management System using C for more hands-on coding examples.
C Source Code
#include <stdio.h>
int main() {
int units;
float totalAmount, surcharge = 0;
printf("Enter the total units consumed: ");
scanf("%d", &units);
if (units <= 100) {
totalAmount = units * 1.50;
} else if (units <= 300) {
totalAmount = 100 * 1.50 + (units - 100) * 2.50;
} else {
totalAmount = 100 * 1.50 + 200 * 2.50 + (units - 300) * 4.00;
}
// Add surcharge if applicable
if (totalAmount > 500) {
surcharge = totalAmount * 0.15;
}
totalAmount += surcharge;
printf("Electricity Bill: Rs. %.2f\n", totalAmount);
return 0;
}
Step-by-Step Code Explanation
The code begins by including the <stdio.h> header, which is necessary for performing standard input/output operations in C.
The main() function is declared, which serves as the entry point of the program.
Three variables are declared: units, totalAmount, and surcharge. These variables will be used to store the units consumed, the calculated electricity bill amount, and any additional surcharge, respectively.
The printf() function is used to display a message asking the user to enter the total units consumed.
The scanf() function is used to read the user's input and store it in the units variable. The %d format specifier is used to indicate that the input is an integer.
Electricity Rate Structure Breakdown
The program then uses conditional statements (if-else) to calculate the totalAmount based on the given rate structure:
| Consumption Slab | Rate per Unit | Formula Used |
|---|---|---|
| First 100 Units (≤ 100) | Rs. 1.50 | totalAmount = units * 1.50; |
| 101 to 300 Units | Rs. 2.50 | totalAmount = 100 * 1.50 + (units - 100) * 2.50; |
| Above 300 Units | Rs. 4.00 | totalAmount = 100 * 1.50 + 200 * 2.50 + (units - 300) * 4.00; |
If the units consumed are less than or equal to 100, the totalAmount is calculated by multiplying the units by the rate of Rs. 1.50 per unit. This is represented by the line totalAmount = units * 1.50;.
If the units consumed are between 101 and 300 (both inclusive), the totalAmount is calculated by adding the cost of the first 100 units at Rs. 1.50 per unit to the cost of the remaining units at Rs. 2.50 per unit. This is represented by the line totalAmount = 100 * 1.50 + (units - 100) * 2.50;.
If the units consumed are greater than 300, the totalAmount is calculated by adding the cost of the first 100 units at Rs. 1.50 per unit, the cost of the next 200 units at Rs. 2.50 per unit, and the cost of the remaining units at Rs. 4.00 per unit. This is represented by the line totalAmount = 100 * 1.50 + 200 * 2.50 + (units - 300) * 4.00;.
Surcharge & Output Formatting
The program then checks if the totalAmount exceeds Rs. 500 using an if statement. If it does, a surcharge of 15% is calculated based on the totalAmount and stored in the surcharge variable. The line surcharge = totalAmount * 0.15; calculates the surcharge.
The surcharge amount is added to the total amount using the line totalAmount += surcharge;.
Finally, the calculated electricity bill is displayed to the user using the printf() function. The line printf("Electricity Bill: Rs. %.2f\n", totalAmount); prints the bill amount, rounded to 2 decimal places, with the appropriate label and formatting.
The return 0; statement indicates that the program has executed successfully and is ready to exit.
Summary
In summary, the code prompts the user to enter the total units consumed, calculates the electricity bill based on the given rate structure, adds a surcharge if necessary, and then displays the final bill amount to the user. For building larger invoice and transaction workflows, check out our guide on creating a Simple Billing Invoice System.

Post a Comment