Concept

How can PHP conditionals validate income and calculate life and taxes?

ComputerScienceOne / Life & Taxes

"38.3.2. Life & Taxes Let’s adapt the conditional statements we developed in Section 3.6.4 into a full PHP script. The first thing we need to do is establish the variables we’ll need and read them in from the user. At the same time we can check for bad input (negative values) for both the inputs. 520\n\n38.3. Examples 1 //prompt for income from the user 2 printf(\"Please enter your Adjusted Gross Income: \"); 3 4 $income = floatval(trim(fgets(STDIN))); 5 6 //prompt for children 7 printf(\"How many children do you have? \"); 8 $numChildren = intval(trim(fgets(STDIN))); 9 10 if($income < 0 || $numChildren < 0) { 11 printf(\"Invalid inputs\"); 12 exit(1); 13 } Next, we can code a series of if-else-if statements for the income range. By placing the ranges in increasing order, we only need to check the upper bounds just as in the original example. 1 if($income <= 18150) { 2 $baseTax = $income * .10; 3 } else if($income <= 73800) { 4 $baseTax = 1815 + ($income -18150) * .15; 5 } else if($income <= 148850) { 6 ... 7 } else { 8 $baseTax = 127962.50 + ($income - 457600) * .396; 9 } Next we compute the child tax credit, taking care that it does not exceed $3,000. A conditional based on the number of children suffices as at this point in the program we already know it is zero or greater. 1 if($numChildren <= 3) { 2 $credit = $numChildren * 1000; 3 } else { 4 $credit = 3000; 5 } Finally, we need to ensure that the credit does not exceed the total tax liability (the credit is non-refundable, so if the credit is greater, the tax should only be zero, not 521"

Related Ideas

How can PHP conditionals validate income and calculate life and taxes? | ComputerScienceOne | Bifalgorithm | Bifalgorithm