Write a C Program to Calculate Electricity Bill with an example. For this, we are using the Else If Statement.
C Program to Calculate Electricity Bill Example
This 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
Finding Electricity Bill Example 2
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 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; }
Comments are closed.