Bifalgorithm post · Computer science one · Life & Taxes

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

How can PHP conditionals validate income and calculate life and taxes? (Computer science one · Life & Taxes) — "38.3.2. Life & Taxes Let’s adapt the conditiona…

Ideas in this flowchart

  • PHP script for calculating income tax and child tax credits
  • Establish and read the required user inputs
  • Read the adjusted gross income as a floating-point value
  • Read the number of children as an integer
  • Check whether either input is negative
  • For invalid inputs, print an error and exit the program
  • Calculate the base tax using income-range conditions
  • Check income ranges in increasing order using upper bounds
  • Apply the formula associated with the matching income bracket
  • Compute the child tax credit
  • Limit the credit to $3,000 based on the number of children
  • Ensure that the credit does not exceed the total tax liability
  • Because the credit is non-refundable, the tax cannot become negative
Feed/

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

No cover

Channel

Computer science one

1 page

"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"

Flowchart preview
👍
0
0 comments0 shares

Sign in to view and post comments.