# C Program to Calculate Electricity Bill

«
»

The electricity bill is calculated by multiplying the unit generated by the cost per unit. ie, if the unit generated is 190(say) and the cost per unit is 10Rs so the total electricity bill is 190×10 = 1900Rs.

Although the electric meters installed at our houses read the electricity consumed in kilowatt-hours(kwh) and 1 kwh is equal to 1 unit.

So during calculation 1kwh and 1unit means the same thing.

Problem Description

Write a C program to calculate electricity bill.

Problem Solution

1. Take input from the user.
2. Calculate the electricity bill using if else ladder.
3. Print the result.

Example:
Suppose the rate of electricity bill is as followed:

• For 1 to 50 units – 30Rs/unit
• For 50 to 100 units – 35Rs/unit
• For 100 to 150 units – 40Rs/unit
• For 150 units and above – 50Rs/unit

So calculate the electricity bill for 300 units?

Note: Join free Sanfoundry classes at Telegram or Youtube

Solution:

Units Cost/Unit Balance Bill
1st 50 30Rs 250 units 50×30 = 1500Rs
2nd 50 35Rs 200 units 50×35 = 1750Rs
3rd 50 40Rs 150 units 50×40 = 2000Rs
Remaining 150 units 50Rs 0 units 150×50 =7500Rs

So the total electricity bill be 1500 + 1750 + 2000 + 7500 = 12,750 Rs.

Program/Source Code

Here is source code of the C program to calculate electricity bill. The C program is successfully compiled and run on a Linux system. The program output is also shown below.

```/*
* C Program to Calculate Electricity Bill
*/

#include <stdio.h>

// 1 to 50 units  - 30Rs/unit
// 50 to 100 units - 35Rs/unit
// 100 to 150 units - 40Rs/unit
// 150 units and above - 50Rs/unit

int electricity_bill(int units)
{
if (units <= 50)
{
return units * 30;
}
else if (units <= 100)
{
return (50 * 30) + (units - 50) * 35;
}
else if (units <= 150)
{
return (50 * 30) + (50 * 35) + (units - 100) * 40;
}
else
{
return (50 * 30) + (50 * 35) + (50 * 40) + (units - 150) * 50;
}
}
int main()
{
int units;
printf("Enter the units: ");
scanf("%d", &units);
printf("The electricity bill is: %d\n", electricity_bill(units));
return 0;
}```
Program Explanation

1. Use the scanf() function to get user input for units and store it in units variable.
2. Create a function named electricity_bill and passed the parameter units entered by the user.
3. using if – else if and else ladder specify the condition that says if units is less than 50 then return product of units and 30, else if the units is less than 100 it means it has to return 1st 50 units at 30 Rs rate and remaining at 35 Rs rate. So return (50 × 30) + (units – 50) × 35 and so on for other ranges.
4. Print the result using printf() function.

Time Complexity: O(1)
The time Complexity to calculate electricity bill is O(1) as the program is taking constant time. ie, no any loop or recursive call is made.

Space Complexity: O(1)
The space Complexity to calculate electricity bill is O(1) as the program require no auxiliary space. ie, no any data structure is required to store the data.

Runtime Test Cases

Testcase 1: In this case, we input “300” as units to calculate the electricity bill. (1 to 50 units – 30Rs/unit, 50 to 100 units – 35Rs/unit, 100 to 150 units – 40Rs/unit and 150 units and above – 50Rs/unit)

```Enter the units: 300
The electricity bill is: 12750```

Testcase 2: In this case, we input “180” as units to calculate the electricity bill. (1 to 50 units – 30Rs/unit, 50 to 100 units – 35Rs/unit, 100 to 150 units – 40Rs/unit and 150 units and above – 50Rs/unit)

```Enter the units: 180
The electricity bill is: 6750```

To practice programs on every topic in C, please visit “Programming Examples in C”, “Data Structures in C” and “Algorithms in C”. 