Home > Blockchain >  C Tax Calculation Program
C Tax Calculation Program

Time:06-25

I am writing a code for Calculation of Tax according to Slabs:

Range ---- Tax Levied

2.5-5 Lakhs ---- 5%

5-10 Lakhs ---- 20%

Above 10 Lakh ---- 30%

No Tax under 2.5 Lakhs.

Code:-

#include<stdio.h>
int main()
{
    float salary, tax_amount = 0;
    printf("Enter the Salary:-");
    scanf("%f", &salary);
    while (salary > 250000)
    {
        if (salary > 250000 && salary <= 500000)
        {
            tax_amount  = (salary - 250000) * 0.05;
            break;
        }
        else if (salary > 500000 && salary >= 1000000)
        {
            tax_amount  = (salary - 500000) * 0.20;
            salary = 500000;
        }
        else
        {
            tax_amount  = (salary - 1000000) * 0.30;
            salary = 1000000;
        }
    }
    printf("Net Tax Amount:-%.3f", tax_amount);
    return 0;
}

This code isn't giving me the desired output? Can anyone tell me the where I went wrong?

CodePudding user response:

I guess you've made an error in your if condition . Try the following :

    #include<stdio.h>
int main()
{
    float salary, tax_amount = 0;
    printf("Enter the Salary:-");
    scanf("%f", &salary);
    while (salary > 250000)
    {
        if (salary > 250000 && salary <= 500000)
        {
            tax_amount  = (salary - 250000) * 0.05;
            break;
        }
        else if (salary > 500000 && salary <= 1000000) // Error here changing >= to <=
        {
            tax_amount  = (salary - 500000) * 0.20;
            salary = 500000;
        }
        else
        {
            tax_amount  = (salary - 1000000) * 0.30;
            salary = 1000000;
        }
    }
    printf("Net Tax Amount:-%.3f", tax_amount);
    return 0;
}
  •  Tags:  
  • c
  • Related