QUESTION #019

Calculate Electricity Bill with Tiered Pricing

Medium

📋 Problem Statement

Given electricity unit charge, calculate the total electricity bill according to the given condition:

Units Range Rate per Unit
For First 50 Units Rs.0.50/unit
For next 100 Units (51-150) Rs.0.75/unit
For next 100 Units (151-250) Rs.1.20/unit
For unit above 250 Rs.1.50/unit
⚡ Important: An additional surcharge of 20% is added to the bill.

📥 Input Format

Integer value indicating the number of units consumed

📤 Output Format

Print the output with floating point value indicating the total cost rounded off to two decimal places

💡 Examples

Example 1:
Input: 541
Output: 787.80
Step-by-step Calculation:
First 50 units: 50 × 0.50 = 25.00
Next 100 units: 100 × 0.75 = 75.00
Next 100 units: 100 × 1.20 = 120.00
Remaining 291: 291 × 1.50 = 436.50
─────────────────────────────────────
Subtotal: 656.50
Surcharge (20%): 656.50 × 0.20 = 131.30
─────────────────────────────────────
Total Bill: 787.80
Example 2:
Input: 300
Output: 372.00
Step-by-step Calculation:
First 50 units: 50 × 0.50 = 25.00
Next 100 units: 100 × 0.75 = 75.00
Next 100 units: 100 × 1.20 = 120.00
Remaining 50: 50 × 1.50 = 75.00
─────────────────────────────────────
Subtotal: 295.00
Surcharge (20%): 295.00 × 0.20 = 59.00
─────────────────────────────────────
Total Bill: 354.00

⚠️ Constraints

-10^9 <= INPUT <= 10^9

✅ Solution

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

int main() {
    double units;
    cin >> units;

    // Handle invalid input
    if (units < 0) {
        cout << "0.00";
        return 0;
    }

    double bill = 0.0;

    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;
    }

    // Adding 20% surcharge
    bill += bill * 0.20;

    // Output rounded to 2 decimal places
    cout << fixed << setprecision(2) << bill;

    return 0;
}

🔑 Key Concepts

  • Tiered Pricing: Different rates for different consumption ranges - common in utility billing
  • Cumulative Calculation: Adding previous tier amounts when in higher tiers
  • Double Data Type: Essential for decimal calculations with rates like 0.50, 0.75, etc.
  • Mathematical Operations: Subtracting tier boundaries (units - 50, units - 150) to get remaining units
  • Percentage Addition: Adding surcharge with bill += bill * 0.20
  • Input Validation: Checking for negative units and handling edge cases
  • Output Formatting: Using fixed and setprecision(2) for 2 decimal places

💡 Understanding Tiered Pricing Logic

The key insight is that higher tiers include all previous tier costs:

  • ≤ 50 units: Only first tier applies
  • 51-150 units: First 50 at 0.50 + remaining at 0.75
  • 151-250 units: First 50 at 0.50 + next 100 at 0.75 + remaining at 1.20
  • > 250 units: All previous tiers + excess at 1.50

Example: For 300 units, you don't pay 300 × 1.50. You pay:

50 × 0.50 + 100 × 0.75 + 100 × 1.20 + 50 × 1.50 = 295.00

⚠️ Common Mistakes to Avoid

  • Wrong Tier Logic: Multiplying all units by the highest tier rate instead of cumulative calculation
  • Incorrect Remaining Units: Not subtracting tier boundaries (e.g., units - 150 for third tier)
  • Integer Division: Using int instead of double loses decimal precision
  • Forgetting Surcharge: Not adding the 20% surcharge at the end
  • Wrong Surcharge Calculation: Using bill * 1.20 (which gives 120% total) instead of bill + bill * 0.20
  • No Input Validation: Not handling negative units or edge cases
  • Missing setprecision: Output won't be formatted to exactly 2 decimal places

🚀 Alternative: Simplified Surcharge

Instead of adding surcharge separately:

// Original method:
bill += bill * 0.20;

// Alternative (multiply by 1.20):
bill = bill * 1.20;

// Or even simpler:
bill *= 1.20;

All three methods give the same result. The last one is most concise!

🎯 What You'll Learn

  • How to implement tiered pricing systems with cumulative calculations
  • Working with floating-point arithmetic for financial calculations
  • Understanding real-world utility billing logic
  • Input validation and error handling
  • Calculating and adding percentage-based surcharges
  • Formatting decimal output with precision control
  • Breaking down complex problems into conditional ranges

🌍 Real-world Applications

  • Utility Bills: Electricity, water, gas - all use tiered pricing
  • Tax Brackets: Income tax systems work similarly with progressive rates
  • Data Plans: Mobile data charges often have tiered pricing
  • Parking Fees: First hour cheap, subsequent hours more expensive
  • Shipping Costs: Weight-based tiered pricing for packages
  • Cloud Services: AWS, Azure charge based on usage tiers