## C++ program to find electricity bills:

In this post, we will learn how to find the *electricity bills* in *C++*. The program will take the number of units as an input from the user, calculate the electricity bill and print the result.

The electricity bill is calculated based on different units. For different units, the cost will be different. In this program, we will take the number of units as input and the program will calculate the bill amount for that unit.

### Algorithm to calculate electricity bills:

To calculate the electricity bill, we need the charge for different unit ranges. This will be different for different regions. In this example, we will consider the below rates:

```
Upto 100 Units : 2
101 to 200 Units : 4
201 to 300 Units : 6
Above 300 Units : 9
```

We will use the below algorithm to do the calculation:

- Take the total units as input from the user. Store this value in a variable.
- If the total units are
*≤100*, the cost will be*total units * 2* - If the total units are
*>100*and*≤200*, the cost will be*(100 * 2) + (total-units - 100) * 4* - If the total units are
*>200*and*≤300*, the cost will be*(100 * 2) + (100 * 4) + (total-units - 200) * 6* - If the total units are
*>300*, the cost will be*(100 * 2) + (100 * 4) + (100 * 6) + (total-units - 300) * 9* - Print the value of cost.

Let’s use this algorithm to write down the program.

### Method 1: Basic C++ program to calculate electricity bills:

Below is the complete program that calculates the *electricity bill* based on a user-input unit value:

```
#include <iostream>
using namespace std;
int main()
{
int totalUnits, cost = -1;
cout << "Enter the total consumed units: " << endl;
cin >> totalUnits;
if (totalUnits > 0 && totalUnits <= 100)
{
cost = totalUnits * 2;
}
else if (totalUnits > 100 && totalUnits <= 200)
{
cost = (100 * 2) + (totalUnits - 100) * 4;
}
else if (totalUnits > 200 && totalUnits <= 300)
{
cost = (100 * 2) + (100 * 4) + (totalUnits - 200) * 6;
}
else if (totalUnits > 300)
{
cost = (100 * 2) + (100 * 4) + (100 * 6) + (totalUnits - 300) * 9;
}
if (cost == -1)
{
cout << "Please enter a valid units value !!";
}
else
{
cout << "Total cost: " << cost << endl;
}
}
```

In this program,

- Two integer variables
*totalUnits*and*cost*are initialized to store the total units and calculated cost. The*cost*variable is initialized as*-1*. - The total consumed unit is read as an input from the user and it is assigned to
*totalUnits*. - The
*if-else if-else*block is calculating the total cost based on the total units. It is using the same formula we discussed above. - At the end of the program, it prints the total calculated cost.

If you run this program, it will give output as like below:

```
Enter the total consumed units:
123
Total cost: 292
```

### Method 2: C++ program to calculate electricity bills by using a separate function:

Let’s use a separate function to calculate the electricity bills. We can move the *if-else if-else* blocks to a separate function and call this function from the *main method*.

```
#include <iostream>
using namespace std;
int calculateElectricityBill(int totalUnits);
int main()
{
int totalUnits, cost;
cout << "Enter the total consumed units: " << endl;
cin >> totalUnits;
cost = calculateElectricityBill(totalUnits);
if (cost == -1)
{
cout << "Please enter a valid units value !!";
}
else
{
cout << "Total cost: " << cost << endl;
}
}
int calculateElectricityBill(int totalUnits)
{
if (totalUnits > 0 && totalUnits <= 100)
{
return totalUnits * 2;
}
else if (totalUnits > 100 && totalUnits <= 200)
{
return (100 * 2) + (totalUnits - 100) * 4;
}
else if (totalUnits > 200 && totalUnits <= 300)
{
return (100 * 2) + (100 * 4) + (totalUnits - 200) * 6;
}
else if (totalUnits > 300)
{
return (100 * 2) + (100 * 4) + (100 * 6) + (totalUnits - 300) * 9;
}
return -1;
}
```

Here, we created a separate function *calculateElectricityBill* to do the electricity bill calculation. It takes the *total units* as the parameter and returns the total cost.

If you run this program, it will print similar outputs.

### Method 3: C++ program to calculate electricity bills by using a separate class:

Another way to write this program is by using a separate class. We can create one object of the class and call its method to calculate the total bill. The advantage of this process is that the same class can be used to calculate the bill from different places.

```
#include <iostream>
using namespace std;
class BillCalculator
{
public:
int calculateElectricityBill(int totalUnits);
};
int BillCalculator::calculateElectricityBill(int totalUnits)
{
if (totalUnits > 0 && totalUnits <= 100)
{
return totalUnits * 2;
}
else if (totalUnits > 100 && totalUnits <= 200)
{
return (100 * 2) + (totalUnits - 100) * 4;
}
else if (totalUnits > 200 && totalUnits <= 300)
{
return (100 * 2) + (100 * 4) + (totalUnits - 200) * 6;
}
else if (totalUnits > 300)
{
return (100 * 2) + (100 * 4) + (100 * 6) + (totalUnits - 300) * 9;
}
return -1;
}
int main()
{
int totalUnits, cost;
BillCalculator calculator;
cout << "Enter the total consumed units: " << endl;
cin >> totalUnits;
cost = calculator.calculateElectricityBill(totalUnits);
if (cost == -1)
{
cout << "Please enter a valid units value !!";
}
else
{
cout << "Total cost: " << cost << endl;
}
}
```

Here,

*BillCalculator*is a class to calculate the total bill.- In the main method, we created an object of
*BillCalculator*,*calculator*at the start of the method. The method*calculateElectricityBill*is called on this object to calculate the electricity bill.

If you run this program, it will print similar output.

```
Enter the total consumed units:
333
Total cost: 1497
```

### You might also like:

- C++ program to convert a hexadecimal value to binary
- C++ STL accumulate method explanation with example
- C++ program to convert hexadecimal to decimal
- C++ program to find the sum of the first and last digits of a number
- C++ program to find all prime numbers to n using sieve of Eratosthenes algorithm
- C++ program to print all subsets of a set
- How to sort an array in ascending order in C++
- C++ program to solve a quadratic equation in different ways
- 3 ways to find the exponent without using pow() method in C++