# How to calculate electricity bill using a C++ program

In this tutorial, we will be learning to calculate the electricity bill. I will be doing this with the help of total number of units consumed.

It is straightforward, and all you need is to have the information about the electricity slab rates. The electricity slab rates vary from state to state and are different for different cities in the same state.

So let’s get started and see how we can easily calculate the electricity bill with a simple c++ program.

## Calculating the electricity bill by the units consumed

As we know, c++ uses OOPS concepts so we will be working through it. I have made a calculate() function, and then I have called that function in the main function. calculate() function takes the number of watts consumed and the duration in months as parameterized input:

```void calculate(int watts, int months)

{

}```

I will calculate the electricity bill with the total number of units consumed. 1 unit = 1kWh. First, we calculate total kWh by multiplying the various parameters. In this way, from the total kWh consumed, we can calculate the corresponding units.

Next, we need to know the slab rate for the units consumed. After that, it is just simple if-else-if statements to categorize the consumption. Depending on the if condition and units consumed, the electricity bill is calculated. However, we must not forget to add the other charges as well in our total bill. These charges include fixed basic charges, regulatory surcharges, tax.

I have shown my code below:

```#include<iostream>
using namespace std;

void calculate(int watts, int months)
{
int hours = 24;
float bill1 = 0.0;
float bill2 = 0.0;
// getting the units consumed
// 1 unit = 1 kWh

float units = watts*hours*(30*months)/(1000.0);

cout << "Total units consumed :" << units << endl;
cout << " " << endl;

//taking the following slab rate as in UP, India:

// 0-150 units - Rs. 5.50/unit
// 151-3000 units - Rs. 6.0/unit
// 301-500 units - Rs. 6.50/unit
// >501 units - Rs. 7.0/unit

cout << "Slab       " << "Rate      " << "Units     " << "Charges       " << endl;
cout << " " << endl;
if (units<=150.0)
{
bill1 = 5.50*units;
bill2 = bill2 + bill1;
cout << "0-150      " << "5.50      " << units << "      "<< bill1 << endl;
cout << " " << endl;
}
else if (units>150.0 && units<=300.0)
{
bill1 = (5.50*150);
bill2 = bill2 + bill1;
cout << "0-150      " << "5.50      " << "150       " << bill1 << endl;
bill1 = (6.0*(units-150.0));
cout << "150-300    " << "6.0       " << units-150 << "        " << bill1 << endl;
bill2 = bill2 + bill1;
cout << " " << endl;

}
else if(units>300.0 && units<=500.0)
{
bill1 = (5.50*150);
bill2 = bill2 + bill1;
cout << "0-150     " << "5.50        " << "150      " << bill1 << endl;
bill1 = (6.0*150);
bill2 = bill2 + bill1;
cout << "150-300   " << "6.0         " << "150      " << bill1 << endl;
bill1 = (6.50*(units-300.0));
bill2 = bill2 + bill1;
cout << "300-500   " << "6.50        " << units-300 << "      " << bill1 << endl;
cout << " " << endl;
}
else if(units>500.0)
{
bill1 = (5.50*150);
bill2 = bill2 + bill1;
cout << "0-150      "<< "5.50      " << "150       " << bill1 << endl;
bill1 = (6.0*150);
bill2 = bill2 + bill1;
cout << "150-300    " << "6.0       " << "150       " << bill1 << endl;
bill1 = (6.50*200);
bill2 = bill2 + bill1;
cout << "300-500    " << "6.50      " << "200       " << bill1 << endl;
bill1 = (7.0*(units-500.0));
bill2 = bill2 + bill1;
cout << ">500       " << "7.0       " << units-500 << "      " <<  bill1 << endl;
cout << " " << endl;

}

float fixed_charge = 110.0;
float regulatory_surcharge = 246.0;
float tax = 332.0;

cout << "  " << endl;
cout << "Final Bill Generated" << endl;
cout << "  " << endl;

cout << "Fixed Charges               " << fixed_charge << endl;
cout << "Units Charges               " << bill2  << endl;
cout << "Regulatory Surcharges       " << regulatory_surcharge << endl;
cout << "Tax                         " << tax << endl;

bill2 = fixed_charge + bill2 + regulatory_surcharge + tax;
cout << "Total Bill                  " << bill2 << endl;

}

int main()
{
calculate(500, 3);
}```

Input:
We pass the watts consumed and the months’ duration as parameterized input into our calculate() function.

Output:

First I have generated a slab wise report to obtain the electricity charges. This report shows charges for each slab and units consumed. At the end, I have printed the final bill with all the other charges. I have displayed the total amount at the end:

```Output:

Total units consumed :1080

Slab        Rate       Units      Charges

0-150       5.50       150        825
150-300     6.0        150        900
300-500     6.50       200        1300
>500        7.0        580        4060

Final Bill Generated

Fixed Charges            110
Units Charges            7085
Regulatory Surcharges    246
Tax                      332
Total Bill               7773```