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 |
#include <iostream> using namespace std; int main() { int units; double billAmount; // Input the number of units consumed cout << "Enter the number of units consumed: "; cin >> units; // Determine the bill amount using a switch statement switch (units / 100) { case 0: // 0-99 units billAmount = units * 1.50; break; case 1: // 100-199 units billAmount = 100 * 1.50 + (units - 100) * 2.00; break; case 2: // 200-299 units billAmount = 100 * 1.50 + 100 * 2.00 + (units - 200) * 3.00; break; case 3: // 300-399 units billAmount = 100 * 1.50 + 100 * 2.00 + 100 * 3.00 + (units - 300) * 4.00; break; default: // 400 or more units billAmount = 100 * 1.50 + 100 * 2.00 + 100 * 3.00 + 100 * 4.00 + (units - 400) * 5.00; break; } // Output the bill amount cout << "The total electricity bill is: $" << billAmount << endl; return 0; } |
Explanation:
- Input:
- The program prompts the user to enter the number of electricity units consumed.
- Switch Statement:
- The
switch
statement checks the number of units consumed divided by 100 (units / 100
). - Each
case
handles a different range of unit consumption:case 0
: For 0-99 units, the rate is $1.50 per unit.case 1
: For 100-199 units, the first 100 units are billed at $1.50 per unit, and the next 100 units at $2.00 per unit.case 2
: For 200-299 units, the first 100 units are billed at $1.50 per unit, the next 100 units at $2.00 per unit, and the next 100 units at $3.00 per unit.case 3
: For 300-399 units, the first 100 units are billed at $1.50 per unit, the next 100 units at $2.00 per unit, the next 100 units at $3.00 per unit, and the next 100 units at $4.00 per unit.default
: For 400 or more units, any units above 400 are billed at $5.00 per unit in addition to the previous tiers.
- The
- Output:
- The program calculates the bill amount based on the units consumed and prints the total bill.
Example Outputs:
Example 1: 50 Units Consumed
Input:
- Units Consumed:
50
Output:
1 2 |
Enter the number of units consumed: 50 The total electricity bill is: $75 |
Example 2: 150 Units Consumed
Input:
- Units Consumed:
150
Output:
1 2 |
Enter the number of units consumed: 150 The total electricity bill is: $250 |
Example 3: 350 Units Consumed
Input:
- Units Consumed:
350
Output:
1 2 |
Enter the number of units consumed: 350 The total electricity bill is: $950 |
Example 4: 500 Units Consumed
Input:
- Units Consumed:
500
Output:
1 2 |
Enter the number of units consumed: 500 The total electricity bill is: $2000 |