C Program to Calculate Electricity Bill

In this post, we will learn how to calculate electricity bill using the C Programming language.

This program asks the user to enter total units consumed, then it calculates the electricity bill accordingly.

So, without further ado, let’s begin this tutorial.

C Program to Calculate Electricity Bill

// C Program to Calculate Electricity Bill
#include <stdio.h>
int main(){
    int units;
    float amount, charge, total_amount;
    
    // Asking for input
    printf("Enter total units consumed: ");
    scanf("%d", &units);
    
    // Calculating electricity bill
    if (units <= 50){
        amount = units * 2.50;
        charge = 30;
    }
    else if (units <= 100){
        amount = units * 3.00;
        charge = 50;
    }
    else if (units <= 200){
        amount = units * 3.50;
        charge = 75;
    }
    else if (units <= 300){
        amount = units * 4.00;
        charge = 100;
    }
    else{
        amount = units * 5.00;
        charge = 125;
    }
    
    // Displaying output
    total_amount = amount + charge;
    printf("Electricity Bill: %.2f", total_amount);
    
    return 0;
}

Output

Enter total units consumed: 375
Electricity Bill: 2000.00

How Does This Program Work ?

    int units;
    float amount, charge, total_amount;

In this program, we have declared one int data type variable and three float data type variables named units, amount, charges and total_amount respectively.

    // Asking for input
    printf("Enter total units consumed: ");
    scanf("%d", &units);

Then, the user is asked to enter the total no. of units consumed.

    // Calculating electricity bill
    if (units <= 50){
        amount = units * 2.50;
        charge = 30;
    }
    else if (units <= 100){
        amount = units * 3.00;
        charge = 50;
    }
    else if (units <= 200){
        amount = units * 3.50;
        charge = 75;
    }
    else if (units <= 300){
        amount = units * 4.00;
        charge = 100;
    }
    else{
        amount = units * 5.00;
        charge = 125;
    }
    // Calculating electricity bill
    if (units <= 50){
        amount = units * 2.50;
        charge = 30;
    }
    else if (units <= 100){
        amount = units * 3.00;
        charge = 50;
    }
    else if (units <= 200){
        amount = units * 3.50;
        charge = 75;
    }
    else if (units <= 300){
        amount = units * 4.00;
        charge = 100;
    }
    else{
        amount = units * 5.00;
        charge = 125;
    }

We calculate the amount by multiplying the units by rates.

    // Displaying output
    total_amount = amount + charge;
    printf("Electricity Bill: %.2f", total_amount);

Then, we calculate the electricity bill by adding the amount computed above with charges. And the electricity bill is displayed on the screen using printf() function.

Here, we have used %.2f format specifier because we want to display the result only till 2 decimal places.

Conclusion

I hope after going through this tutorial, you understand how to calculate electricity bill using C Programming language. 

If you have any doubt regarding the tutorial, feel free to contact us in the comment section. We will be delighted to solve your doubt.

1 thought on “C Program to Calculate Electricity Bill”

Leave a Comment

Your email address will not be published. Required fields are marked *