C program to calculate electricity bill

Here's a C program that calculates the electricity bill based on the units consumed:

#include <stdio.h>

float calculateElectricityBill(int units) {
    float billAmount;

    if (units <= 50) {
        billAmount = units * 0.50;
    } else if (units <= 150) {
        billAmount = 25 + (units - 50) * 0.75;
    } else if (units <= 250) {
        billAmount = 100 + (units - 150) * 1.20;
    } else {
        billAmount = 220 + (units - 250) * 1.50;
    }

    return billAmount;
}

int main() {
    int units;
    float billAmount;

    printf("Enter the units consumed: ");
    scanf("%d", &units);

    billAmount = calculateElectricityBill(units);

    printf("Electricity Bill Amount: %.2f\n", billAmount);

    return 0;
}

In this program, we have a function calculateElectricityBill that takes the units consumed as input and returns the bill amount. The calculation of the bill amount is based on the slab rates provided in the program.

Inside the calculateElectricityBill function, we use if-else conditions to determine the slab and calculate the bill amount accordingly. The slab rates are as follows:

  • For the first 50 units, the rate is 0.50 per unit.
  • For the next 100 units (51-150), the rate is 0.75 per unit.
  • For the next 100 units (151-250), the rate is 1.20 per unit.
  • For units above 250, the rate is 1.50 per unit.

In the main function, the user is prompted to enter the units consumed. The calculateElectricityBill function is called with the entered units to compute the bill amount. Finally, the bill amount is displayed using the printf function.

You can compile and run this program, entering the units consumed, to calculate the electricity bill amount based on the slab rates provided.

Prasun Barua

Prasun Barua is an Engineer (Electrical & Electronic) and Member of the European Energy Centre (EEC). His first published book Green Planet is all about green technologies and science. His other published books are Solar PV System Design and Technology, Electricity from Renewable Energy, Tech Know Solar PV System, C Coding Practice, AI and Robotics Overview, Robotics and Artificial Intelligence, Know How Solar PV System, Know The Product, Solar PV Technology Overview, Home Appliances Overview, Tech Know Solar PV System, C Programming Practice, etc. These books are available at Google Books, Google Play, Amazon and other platforms.

*

Post a Comment (0)
Previous Post Next Post