Home

A program to calculate electricity bill by reading units consumed

C Programming Language 021

The following code shows how to write a program to calculate electricity bill by reading units consumed.

Program

/**
 * Units >= 250 - Flat charge 1000$
 * 150 <= Units < 250 - Charge = 200 + units*1.50
 * 100 <= Units < 150 - Charge = 100 + units*1.00
 *  50 <= Units < 100 - Charge =  50 + units*0.50
 *        Units <  50 - Flat charge 10$
 */

#include <stdio.h>

void main()
{
    int uc;
    float bill;
    printf("Enter the number of units consumed:\n");
    scanf("%d", &uc);

    if (uc >= 250)
        bill = 1000;
    else if (uc < 250 && uc >= 150)
        bill = 200 + uc * 1.50;
    else if (uc < 150 && uc >= 100)
        bill = 100 + uc * 1.00;
    else if (uc < 100 && uc >= 50)
        bill = 50 + uc * 0.50;
    else
        bill = 10;

    printf("Amount to be paid = %f", bill);
    printf("\n");
}

Result

Enter the number of units consumed:
55
Amount to be paid = 77.500000


Last Updated on

Next Post: Rules vs Guidelines →

Comments