Finding Discounted Amount Using Else If in C

Definition:

The else if statement is used when there are multiple conditions to check. It evaluates each condition one by one, and the first condition that is true is executed. If none of the conditions are true, the else block is executed.

Syntax:

if(condition1)
{
    // statements
}
else if(condition2)
{
    // statements
}
else if(condition3)
{
    // statements
}
else
{
    // statements
}

Explanation:

This program accepts the purchase amount from the user. Based on the entered amount, it checks different ranges using the else if statement and assigns a discount percentage.

  • If the amount is between ₹300 and ₹1000, a 20% discount is applied.
  • If the amount is between ₹1001 and ₹1500, a 30% discount is applied.
  • If the amount is between ₹1501 and ₹1999, a 40% discount is applied.
  • If the amount is less than ₹300 or ₹2000 and above, no discount is given.

The program then calculates the discount amount and displays the final amount after deducting the discount.


🔹 Program: Finding Discounted Amount

Explanation: This program accepts the purchase amount from the user, determines the discount percentage using the else if statement, calculates the discount amount, and displays the final payable amount.


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

main()
{

    float b,d,a,c;

    clrscr();

    printf("\nEnter Amount : ");
    scanf("%f",&b);

    if(b >= 300 && b <= 1000)
    {
        d = 20;
    }

    else if(b >= 1001 && b <= 1500)
    {
        d = 30;
    }

    else if(b >= 1501 && b < 2000)
    {
        d = 40;
    }

    else
    {
        d = 0;
    }

    printf("\nDiscount Percentage = %.2f",d);

    c = b * d / 100;
    a = b - c;

    printf("\nDiscount Amount = %.2f",c);

    printf("\nAmount After Discount = %.2f",a);

    getch();

}


Practice Exercises

Task 1: Write a C program to calculate the bonus of an employee based on the following salary conditions:

Salary ₹20,000 to ₹30,000: Bonus = 10%
Salary ₹30,001 to ₹50,000: Bonus = 15%
Salary Above ₹50,000: Bonus = 20%
Below ₹20,000: No Bonus

Goal: Learn how to use the else if statement to check multiple salary ranges and calculate the bonus amount.
💡 Hint: Calculate the bonus using the formula Bonus = Salary × Bonus Percentage / 100.
💡 Show Solution

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

main()
{
    float salary, bonus, amount;

    clrscr();

    printf("Enter Salary : ");
    scanf("%f",&salary);

    if(salary >= 20000 && salary <= 30000)
    {
        bonus = 10;
    }
    else if(salary > 30000 && salary <= 50000)
    {
        bonus = 15;
    }
    else if(salary > 50000)
    {
        bonus = 20;
    }
    else
    {
        bonus = 0;
    }

    amount = salary * bonus / 100;

    printf("\nBonus = %.2f",amount);

    getch();
}

Output
Enter Salary : 40000

Bonus = 6000.00
Explanation: The program accepts the employee's salary, checks the salary range using the else if statement, calculates the bonus percentage, and displays the bonus amount.