Electricity Bill Calculator - Problem
Calculate the electricity bill for a given number of units consumed using a slab-based pricing system:
- First 100 units: $1.0 per unit
- Next 200 units (101-300): $1.5 per unit
- Above 300 units: $2.0 per unit
Given the number of units consumed, return the total bill amount.
Note: The billing is cumulative - you pay different rates for different portions of your consumption.
Input & Output
Example 1 — Basic Case (Within Second Slab)
$
Input:
units = 250
›
Output:
325.0
💡 Note:
First 100 units: 100 × $1.0 = $100. Next 150 units: 150 × $1.5 = $225. Total: $100 + $225 = $325
Example 2 — Small Consumption (First Slab Only)
$
Input:
units = 75
›
Output:
75.0
💡 Note:
Only 75 units consumed, all in first slab: 75 × $1.0 = $75
Example 3 — High Consumption (All Three Slabs)
$
Input:
units = 450
›
Output:
725.0
💡 Note:
First 100: $100, Next 200: $300, Remaining 150: 150 × $2.0 = $300. Total: $100 + $300 + $300 = $700
Constraints
- 1 ≤ units ≤ 106
- Return value should be a floating-point number
Visualization
Tap to expand
💡
Explanation
AI Ready
💡 Suggestion
Tab
to accept
Esc
to dismiss
// Output will appear here after running code