C PROGRAMMING PROGRAM • DECISION MAKING

Calculate an Electricity Bill

Learn how to calculate an electricity bill using a clear C program.

IntermediateSlab calculationelse-ifBilling

PROBLEM UNDERSTANDING

Input and expected output

Sample input
250
Sample output
Bill amount = 600.00

COMPLETE C PROGRAM

Complete C implementation

electricity-bill.c
Open in compiler
#include <stdio.h>

int main(void)
{
    double units, bill;

    printf("Enter consumed units: ");
    scanf("%lf", &units);
    if (units < 0) {
        printf("Invalid units\n");
        return 0;
    }
    if (units <= 100)
        bill = units * 1.50;
    else if (units <= 200)
        bill = 100 * 1.50 + (units - 100) * 2.50;
    else
        bill = 100 * 1.50 + 100 * 2.50 + (units - 200) * 4.00;
    printf("Bill amount = %.2f\n", bill);
    return 0;
}

GUIDED CODE TOUR • NOT LIVE EXECUTION

Study the program line by line

Use the real compiler button above to run and debug with different inputs.

CURRENT STEP

Select Start to walk through the important lines.

SELECTED LINE

No line selected

EXPECTED OUTPUT FOR THE SAMPLE

Bill amount = 600.00
0%Step 0 of 0

PROGRAM EXPLANATION

Algorithm and explanation

  1. Read the required input values.
  2. Accumulate the cost of each completed slab and charge only the remaining units at the current rate.
  3. Display the computed result.

Accumulate the cost of each completed slab and charge only the remaining units at the current rate.

EFFICIENCY

Time and space complexity

Time complexity

O(1)

Auxiliary space

O(1)

DEBUGGING CHECKLIST

Common mistakes

Check this

Use the correct format specifier for every variable.

Check this

Initialize variables before using their values.

Check this

Check braces, semicolons and input order carefully.

Try it yourself

Practice: Run the program with the sample input, predict its output, and then test one boundary case of your own.