Program: Calculate Electricity Bill Using Else If

Explanation: This program accepts the number of electricity units consumed by the user. It determines the rate per unit using the else if statement and then calculates the total electricity bill.


#include <stdio.h>
#include <conio.h>

main()
{
    float u,amt,rate;

    clrscr();

    printf("Enter Units : ");
    scanf("%f",&u);

    if(u >= 1 && u <= 100)
    {
        rate = 5;
    }
    else if(u >= 101 && u <= 200)
    {
        rate = 6;
    }
    else if(u >= 201 && u <= 300)
    {
        rate = 7;
    }
    else
    {
        rate = 8;
    }

    amt = rate * u;

    printf("\nRate per Unit = %.2f",rate);
    printf("\nTotal Bill = %.2f",amt);

    getch();
}

Output:

Enter Units : 250

Rate per Unit = 7.00
Total Bill = 1750.00

Practice Exercises

Task 1: Write a C program to accept the percentage of a student and display the grade according to the following conditions:

A Grade: 80% and above
B Grade: 60% to 79%
C Grade: 40% to 59%
Fail: Below 40%

Goal: Learn how to use the else if statement to test multiple conditions.
💡 Hint: Compare the percentage using multiple else if conditions.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

main()
{
    float per;

    clrscr();

    printf("Enter Percentage : ");
    scanf("%f",&per);

    if(per >= 80)
    {
        printf("\nGrade A");
    }
    else if(per >= 60)
    {
        printf("\nGrade B");
    }
    else if(per >= 40)
    {
        printf("\nGrade C");
    }
    else
    {
        printf("\nFail");
    }

    getch();
}

Output
Enter Percentage : 72

Grade B
Explanation: The program accepts the student's percentage and checks multiple conditions using the else if statement. According to the percentage entered, it displays the appropriate grade (A, B, C, or Fail).