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
INPUTALGORITHMRESULTUnits Consumed250Electricity units tocalculate bill for1Check Slab Ranges0-100: $1.0 | 101-300: $1.5 | 301+: $2.02Calculate Each SlabFirst 100: 100×$1=$100Next 150: 150×$1.5=$2253Sum All SlabsTotal = $100 + $225 = $325Electricity Bill$325.0Final amount to payfor 250 units💡Key Insight:Slab-based billing requires calculating cost for each tier separately,then summing them up based on consumption ranges.TutorialsPoint - Electricity Bill Calculator | Conditional Slabs
Asked in
Accenture 15 TCS 12
32.9K Views
Medium Frequency
~8 min Avg. Time
890 Likes
Ln 1, Col 1
Smart Actions
💡 Explanation
AI Ready
💡 Suggestion Tab to accept Esc to dismiss
// Output will appear here after running code
Code Editor Closed
Click the red button to reopen