# Calculate Electricity Bills in C Program

In this tutorial, we are going to create a electricity bill calculate . this program is designed to help you calculate your electricity bill based on the units consumed. Let's see.

``````
#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;
}

if (totalAmount > 500) {
surcharge = totalAmount * 0.15;
}

totalAmount += surcharge;

printf("Electricity Bill: Rs. %.2f\n", totalAmount);

return 0;
}

``````

• The code begins by including the "<stdio.h>" header, which is necessary for performing 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.

• The program then uses conditional statements (if-else) to calculate the totalAmount based on the given rate structure:

• 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;".

• 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 totalAmount 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.

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.