C program to calculate electricity bill

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.

Learn More: C program to calculate electricity bill

要查看或添加评论,请登录

Prasun Barua的更多文章

社区洞察

其他会员也浏览了