Calculate weekly net pay

Write a program that requests the hours worked in a week and then prints the gross pay, the taxes, and the net pay. Assume the following:

1. Basic pay rate = $10.00/hr 2. Overtime (in excess of 40 hours) = time and a half 3. Tax rate: 15% of the first$300
• 20% of the next \$150
• 25% of the rest

Is my code way too long and complicated for that?

#include <stdio.h>
#define PAYRATE 10 //basic pay rate per hour
#define OVERTIME 15 //in excess of 40 hours a week
int NetPay(int hours);

int main()

{
int userWeeklyHours;

scanf("%d", &userWeeklyHours);// get the user weekly hours
NetPay(userWeeklyHours);

return 0;
}

int NetPay(int hours)// implementing the function to calculate total pay, total taxes, and net pay

{
int firstRate, secondRate, restOfRate, secondAmount, rest, payAfterTax, payedBeforeTax, overHours;

if (hours > 40)
{
overHours = hours - 40;
payedBeforeTax = (40 * PAYRATE) + (overHours * OVERTIME);
}

else
payedBeforeTax = hours * PAYRATE;//defining the user first paycheck before taxes

if (payedBeforeTax <= 300)//paying only first rate case
{
firstRate = payedBeforeTax*0.15;
payAfterTax =payedBeforeTax - firstRate;
printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate, payAfterTax);
}

else if (payedBeforeTax > 300 && payedBeforeTax <= 450)//paying first and second rate
{
secondAmount = payedBeforeTax - 300;
firstRate = (payedBeforeTax - secondAmount) * 0.15;
secondRate = secondAmount * 0.20;
payAfterTax = payedBeforeTax - (firstRate + secondRate);
printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate, payAfterTax);

}

else if (payedBeforeTax > 450)// paying all rates
{
rest = payedBeforeTax - 450;
secondAmount = (payedBeforeTax - 300) - rest;
firstRate = (payedBeforeTax - (rest + secondAmount)) * 0.15;
secondRate = secondAmount * 0.20;
restOfRate = rest * 0.25;
payAfterTax = payedBeforeTax - (firstRate + secondRate + restOfRate);
printf("your total gross is %d, your taxes to pay are %d, your net pay is %d", payedBeforeTax, firstRate + secondRate + restOfRate, payAfterTax);

}

return payAfterTax;
}

-

I'd start extracting some more functions.

int NetPay(int hours){
int payedBeforeTaxes = payedBeforeTaxes(hours);
return applyTaxes(payedBeforeTaxes);
}

int payedBeforeTaxes(hours){
return regularPay(hours) + overtimePay(hours);
}

int applyTaxes(int amountBeforeTaxes){
int firstRateTaxes  = applyFirstRate(amountBeforeTaxes);
int secondRateTaxes = applySecondRate(amountBeforeTaxes);
int thirdRateTaxes  = applyThirdRate(amountBeforeTaxes);
return amountBeforeTaxes - firstRateTaxes -
secondRateTaxes - thirdRateTaxes;
}


At this point you just need to implement the functions with the appropriate code. You'll notice that with this structure each function only does one thing and it will be much simpler and clearer.

Alternatively you can replace your apply taxes introducing an array of `(rate, threshold). You will have to loop through the array, and at each step compute the amount you have to apply that tax rate and the associated tax amount.

-
+1 for separating regular/overtime pay and separating the rates. It's always better to wrap the solution around the problem than the other way around. – Ryan Gates Jan 29 '13 at 17:08
thank you very much @mariosangiorgio – Nir Jan 29 '13 at 17:18