Java ProgramsControl FlowCalculate Electricity Bill Using Conditions

Calculate Electricity Bill Using Conditions in Java

intermediate·  Control Flow  ·  Conditional Statements

Problem

Tiered electricity billing charges a different rate for each block of units consumed, similar to a progressive tax slab — this example uses simplified, illustrative rates rather than any specific utility's published tariff.

Given the number of units consumed, calculate the total electricity bill using tiered per-unit rates.

Input
units = 350
Output
Electricity bill: 1650.0

Java Program

Java
public class ElectricityBillCalculator { public static void main(String[] args) { int units = 350; double bill; if (units <= 100) { bill = units * 3.0; } else if (units <= 300) { bill = 100 * 3.0 + (units - 100) * 5.0; // full first tier plus the portion in this tier } else { bill = 100 * 3.0 + 200 * 5.0 + (units - 300) * 7.0; // full first two tiers plus the remainder } System.out.println("Electricity bill: " + bill); } }

Output

Electricity bill: 1650.0

Core Logic

Each tier only bills the units that fall within its own block, so higher tiers add their rate on top of the full cost already computed for the tiers below them.

How It Works
  1. 1Up to 100 units are billed at a flat rate of 3 per unit.
  2. 2Units from 101 to 300 add the full cost of the first 100 units, plus 5 per unit for everything in this tier.
  3. 3Units above 300 add the full cost of the first two tiers, plus 7 per unit for the remainder.
  4. 4The bracket a candidate's unit count falls into determines how many tiers' worth of cost get added together.
For units = 350: the first 100 units cost 300, the next 200 cost 1000, and the remaining 50 cost 350 — totaling 1650.
💡

Key Point: Each tier's rate only applies to the units within that specific block, not the whole consumption — that's what makes this a genuine tiered bill rather than a single flat rate.

Key Concepts

if / else iftiered ratesarithmetic

Related Programs