0

I've got this piece of code which calculates the monthly tax of an employee's salary. When I run it, everything seems to work fine until the point in the if clause.

If I supply basicSalary as 50000 and all the other input values as 0, the monthlyTax figure comes zero when it should be around 4000.

Could anybody explain me why this is happening?

#include <stdio.h>

int main()
{
    int basicSalary, allowances, transportAllowance, numberOfDependants, deduction;
    float monthlyTax, income;

    printf("Enter Basic Salary Amount: ");
    scanf("%d", &basicSalary);

    printf("\nEnter Allowances Amount: ");
    scanf("%d", &allowances);

    printf("\nEnter transportAllowance Amount: ");
    scanf("%d", &transportAllowance);

    printf("\nEnter Number Of Dependants: ");
    scanf("%d", &numberOfDependants);

    switch (numberOfDependants)
    {
           case 0:
                deduction = 215000;
                break;
           case 1:
                deduction = 325000;
                break;
           case 2:
                deduction = 415000;
                break;
           case 3:
                deduction = 475000;
                break;
           default:
                   printf("Number Of Dependants Can Only Be Between 0 - 3, Enter A Proper Value.");
                   return 1;
    }

    income = basicSalary * 13 + allowances + (transportAllowance - 6800) * 12 - deduction;

    if (income < 500000)
    {
        monthlyTax = ((15/100) * (income/12));
    }
    else
    {  
        monthlyTax = ((15/100) * (500000/12)) + ((30/100) * ((income-500000)/12));
    }

    monthlyTax = monthlyTax/12;

    printf("\nMothly Tax Amount is %f", monthlyTax);
    getch();

    return 0;
}
4
  • 3
    That's for being fancy and using 15/100 instead of a more "grounded" 0.15 ;-) Feb 15, 2013 at 18:08
  • 1
    And in C, it should be int main(void) because () is NOT equivalent to (void) in a parameter declaration. Oh, and please stop spreading the disease of printing newlines at the beginning of a line; they belong at the end of the line printed.
    – Jens
    Feb 15, 2013 at 18:09
  • thank you to you both! :) @dasblinkenlight - yup, got too fancy there :P
    – Hirvesh
    Feb 15, 2013 at 18:11
  • You are using integer variables, and trying to compute real (floating point) values. If you want to do "exact" arithmetic with dollars and cents, compute in cents (but be careful with the order or operations, in C say 5 / 10 == 0, as integers).
    – vonbrand
    Feb 15, 2013 at 18:29

2 Answers 2

6

In C, 15 / 100 equals 0, because it is integer division.

The original author probably means floating point division 15.0 / 100.0.

Generally speaking, all the constants implied in floating point calculation should also be of floating point type (i.e., have the .0 appended), unless you really know what you are doing. That goes for all the numbers, not just the ones in the divisions, just to be safe.

If they are not constants but integer variables, you may need a cast:

(float)basicSalary ...

And BTW, many of the variables, such as basicSalary, should also be of type float.

And as a last advise, it's generally recommended to use double instead of float everywhere by default, unless you have a specific need.

2

This is caused by integer division

monthlyTax = ((15/100) * (income/12));

Here, 15/100 doesn't evaluate to 0.15, but rather 0 (decimal part stripped off).

Change your formulas to use floating point values:

monthlyTax = ((15/100.f) * (income/12.f));

or

monthlyTax = ((15/100.0) * (income/12.0));
1
  • I don't think 100f is a valid C expression. You should say 100.0f.
    – rodrigo
    Feb 15, 2013 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.