Write a C Program to Calculate Electricity Bill with an example. For this, we are using the Else If Statement. In this program, we use the slab rates as shown below.
- First 50 units: unit charge = 2.60 and a fixed sur charge = 25.
- 50 to 100 units: unit charge = 3.25 and a fixed sur charge = 35.
- 100 to 200 units: unit charge = 5.26 and a fixed sur charge = 45.
- Above 200 units: unit charge = 7.75 and a fixed sur charge = 55.
C Program to Calculate Electricity Bill Example
This C program helps the user to enter the units that a user consumes. Then, it will calculate the Total. This approach to Calculating the Electricity Bill is useful if the board charges different tariffs for different units. For this example, we are using the Else If statement.
#include <stdio.h>
int main()
{
int Units;
float Amount, Sur_Charge, Total_Amount;
printf("\n Please Enter the Units that you Consumed : ");
scanf("%d", &Units);
if (Units < 50)
{
Amount = Units * 2.60;
Sur_Charge = 25;
}
else if (Units <= 100)
{
// First Fifty Units charge is 130 (50 * 2.60)
// Next, we are removing those 50 units from total units
Amount = 130 + ((Units - 50 ) * 3.25);
Sur_Charge = 35;
}
else if (Units <= 200)
{
// First Fifty Units charge is 130, and 50 - 100 is 162.50 (50 * 3.25)
// Next, we are removing those 100 units from total units
Amount = 130 + 162.50 + ((Units - 100 ) * 5.26);
Sur_Charge = 45;
}
else
{
// First Fifty Units 130, 50 - 100 is 162.50, and 100 - 200 is 526 (100 * 5.65)
// Next, we are removing those 200 units from total units
Amount = 130 + 162.50 + 526 + ((Units - 200 ) * 7.75);
Sur_Charge = 55;
}
Total_Amount = Amount + Sur_Charge;
printf("\n Electricity Bill = %.2f", Total_Amount);
return 0;
}

Let me try another value.
Please Enter the Units that you Consumed : 265
Electricity Bill = 1377.25
In the above example, the user consumes 265 units of electricity.
- First 50 units = 130 (50 * 2.60).
- 50 to 100 units = 162.50 (50 * 3.25).
- 100 to 200 units = 526 (100 * 5.26)
- Remaining 65 = 503.75 (65 * 7.75)
- Total = 1322.25 + 55 Sur charge = 1377.25
Program to calculate Electricity Bill using else if
This program to find electricity bills is useful if the board has uniform rates. Something like: if you consume between 300 and 500 units, then changes will be fixed as 7.75 Rupees for a Unit, etc.
The C Else If statement in C Programming will check for the first condition. If it is TRUE, it will execute the statements present in that block. If the condition is FALSE, it will check the Next one (Else If condition) and so on.
#include <stdio.h>
int main()
{
int Units;
float Amount, Sur_Charge, Total_Amount;
printf("\n Please Enter the Units that you Consumed : ");
scanf("%d", &Units);
// If it is True then Statements inside this block will be executed
if (Units > 500)
{
Amount = Units * 9.65;
Sur_Charge = 85;
} // Otherwise (fails), Compiler will move to Next Else If block
else if (Units >= 300)
{
Amount = Units * 7.75;
Sur_Charge = 75;
}
else if (Units >= 200)
{
Amount = Units * 5.26;
Sur_Charge = 55;
}
else if (Units >= 100)
{
Amount = Units * 3.76;
Sur_Charge = 35;
} // Otherwise (fails), Compiler will move to Else block
else
{
Amount = Units * 2.25;
Sur_Charge = 25;
}
Total_Amount = Amount + Sur_Charge;
printf("\n Electricity Bill = %.2f", Total_Amount);
return 0;
}

C Program to Calculate the electricity bill using a for loop
In the following example, we used two separate arrays to store the unit prices and the Unit values. Next, the for loop iterates over the range array. Within the array, the if-else statement checks whether the given number is within the range. If true, multiply the units by the unit price and display the result. Refer to the for loop in C and Array in C articles.
Here, the break statement allows the compiler to exit from the for loop. Otherwise, the else block subtracts the first set of units from the original consumed ones and calculates the electricity bill for those units. The process repeats until it is complete. If the slab rates per unit are more complex, this program is readable and manageable.
#include <stdio.h>
#include <limits.h>
int main()
{
int Units;
float Amount = 0.0;
int range[4] = {50, 50, 100, INT_MAX};
float charges[4] = {2.60, 3.25, 5.26, 7.75};
printf("Enter Consumed Units = ");
scanf("%d", &Units);
for (int i = 0; i < 4; i++)
{
if (Units <= range[i])
{
Amount += charges[i] * Units;
break;
}
else
{
Amount += (charges[i] * range[i]);
Units -= range[i];
}
}
printf("Electricity Bill = %.2f", Amount);
return 0;
}
Enter Consumed Units = 450
Electricity Bill = 2756.00
Calculate electricity bill using switch case
In the following program, we use the switch statement to calculate the electricity bill. Price (in dollars) breakdown for 250 units. Refer to the C switch case.
- 100 * 0.75 = 75
- 100 * 1.5 = 150
- 50 * 2.75 = 137.5
#include <stdio.h>
int main()
{
int Units;
float Amount = 0;
printf("Enter the Units Consumed = ");
scanf("%d", &Units);
switch (Units / 100)
{
case 0:
Amount = Units * 0.75;
break;
case 1:
Amount = 100 * 0.75 + (Units - 100) * 1.5;
break;
case 2:
Amount = 100 * 0.75 + 100 * 1.5 + (Units - 200) * 2.75;
break;
default:
Amount = 100 * 0.75 + 100 * 1.5 + 100 * 2.75 + (Units - 300) * 4.0;
break;
}
printf("Electricity Bill = $%.2f", Amount);
}
Enter the Units Consumed = 250
Electricity Bill = $362.50
Also Read
Comments are closed.