Reputation: 3
when grossPay pass into find tax amount,seem like tax if else doesn't receive it...result is 0.0000 how can i solve this??
float grossPay = 400;
float taxPhase1 = 0;
float taxPhase2 = 0;
float taxPhase3 = 0;
float totalTax = 0;
if(grossPay<=300)
{
taxPhase1 = ((float)(grossPay)*(15/100));
totalTax = taxPhase1;
}
else if(grossPay>300 && grossPay<=450)
{
taxPhase1 = ((float)300*(15/100));
taxPhase2 = ((float)(grossPay-300)*(20/100));
totalTax = (taxPhase1 + taxPhase2);
}
else if(grossPay>450)
{
taxPhase1 = ((float)300*(15/100));
taxPhase2 = ((float)150*(20/100));
taxPhase3 = ((float)(grossPay-450)*(25/100));
totalTax = (taxPhase1 + taxPhase2 + taxPhase3) ;
}
printf("%f",totalTax);
Upvotes: 0
Views: 41
Reputation: 23208
Code has several instances of integer division errors such as:
15/100
Which all result in zero, resulting in incorrect evaluations
taxPhase1 = ((float)(grossPay)*(15/100));//result == 0
Change to
15.0/100 or 15/100.0 (for all occurrences.)
taxPhase1 = ((float)(grossPay)*(15/100.0));//result = 60.0 (gross pay == 400)
Upvotes: 1