# Using the user define functions C++ Program to take a value from the user as input electricity unit charges and calculate total electricity bill according to the given condition: For the first 50 units Rs. 0.50/unit………….For the next 100 units Rs. 0.75/unit…..For the next 100 units Rs. 1.20/unit ……………For unit above 250 Rs. 1.50/unit………An additional surcharge of 20% is added to the bill…….Using user define functions.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> using namespace std; // Function to calculate the total bill using pass-by-value double calculateBillByValue(double units) { double bill; if (units <= 50) { bill = units * 0.50; } else if (units <= 150) { bill = (50 * 0.50) + ((units - 50) * 0.75); } else if (units <= 250) { bill = (50 * 0.50) + (100 * 0.75) + ((units - 150) * 1.20); } else { bill = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units - 250) * 1.50); } // Apply additional surcharge of 20% bill += 0.20 * bill; return bill; } // Function to calculate the total bill using pass-by-reference void calculateBillByReference(double units, double &totalBill) { if (units <= 50) { totalBill = units * 0.50; } else if (units <= 150) { totalBill = (50 * 0.50) + ((units - 50) * 0.75); } else if (units <= 250) { totalBill = (50 * 0.50) + (100 * 0.75) + ((units - 150) * 1.20); } else { totalBill = (50 * 0.50) + (100 * 0.75) + (100 * 1.20) + ((units - 250) * 1.50); } // Apply additional surcharge of 20% totalBill += 0.20 * totalBill; } int main() { double units; double billByValue, billByReference; // Input the number of units cout << "Enter the number of electricity units: "; cin >> units; // Calculate the bill using pass-by-value billByValue = calculateBillByValue(units); cout << "Total bill calculated using pass-by-value: Rs. " << billByValue << endl; // Calculate the bill using pass-by-reference calculateBillByReference(units, billByReference); cout << "Total bill calculated using pass-by-reference: Rs. " << billByReference << endl; return 0; } |

### Explanation:

**Pass-by-Value (**:`calculateBillByValue`

)**Function Definition**:`double calculateBillByValue(double units)`

- This function receives a copy of the
`units`

as input. - It calculates the electricity bill based on the given unit rates:
- For the first 50 units: Rs. 0.50/unit
- For the next 100 units: Rs. 0.75/unit
- For the next 100 units: Rs. 1.20/unit
- For units above 250: Rs. 1.50/unit

- An additional surcharge of 20% is applied to the calculated bill.
- The function returns the final bill amount.

**Pass-by-Reference (**:`calculateBillByReference`

)**Function Definition**:`void calculateBillByReference(double units, double &totalBill)`

- This function also receives the
`units`

but takes a reference to`totalBill`

to store the result. - It performs the same calculations as
`calculateBillByValue`

but updates the`totalBill`

variable directly. - An additional surcharge of 20% is applied to the calculated bill.

**Main Function (**:`main`

)- The program prompts the user to input the number of electricity units.
- It calculates the total bill using both methods and prints the results.

### Example Outputs:

**Example 1:**

**Input:**

- Units:
`120`

**Output:**

1 2 3 |
Enter the number of electricity units: 120 Total bill calculated using pass-by-value: Rs. 112.00 Total bill calculated using pass-by-reference: Rs. 112.00 |

**Example 2:**

**Input:**

- Units:
`300`

**Output:**

1 2 3 |
Enter the number of electricity units: 300 Total bill calculated using pass-by-value: Rs. 398.00 Total bill calculated using pass-by-reference: Rs. 398.00 |

**Example 3:**

**Input:**

- Units:
`80`

**Output:**

1 2 3 |
Enter the number of electricity units: 80 Total bill calculated using pass-by-value: Rs. 83.00 Total bill calculated using pass-by-reference: Rs. 83.00 |