-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path015.2_taxes_net_pay_with_gross_pay.c
51 lines (40 loc) · 1.17 KB
/
015.2_taxes_net_pay_with_gross_pay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
// with functions, with struct
#include <stdio.h>
struct taxBlock {
int beginningOfBlock;
int endingOfBlock;
float taxForBlock;
};
struct taxBlock arr[] = {{1, 300000, 0.00}, {300001, 400000, 0.05}, {400001, 550000, 0.10}, {550001, 750000, 0.15}, {750001, 999999999, 0.20}};
float calcTaxes(float grossPay) {
float tax = 0.0;
int index;
float runningTotal;
for (index = 0, runningTotal = grossPay; runningTotal != 0; index++) { // runningTotal = 550000, 250000
int bob = arr[index].beginningOfBlock;
int eob = arr[index].endingOfBlock;
float tfb = arr[index].taxForBlock;
int slab = (eob - bob + 1);
if (runningTotal <= slab) {
tax += (runningTotal * tfb);
break;
} else {
tax += (slab * tfb);
runningTotal -= slab;
}
}
return tax;
}
float calcNetPay(float grossPay) {
return grossPay - calcTaxes(grossPay);
}
int main() {
float grossPay;
printf("Enter the gross pay: ");
scanf("%f", &grossPay);
printf("\n");
printf("\n~*~*~*~*~*~*~*~*~*~*~*~*~ \n \n");
printf("Gross Pay: %.2f \n", grossPay);
printf("Taxes: %.2f \n", calcTaxes(grossPay));
printf("Net Pay: %.2f \n", calcNetPay(grossPay));
}