Concept

How can conditional statements be used to build a life and taxes program?

ComputerScienceOne / Life & Taxes

"Let’s adapt the conditional statements we developed in Section 3.6.4 into a full C program. 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.\n\n1 double income, baseTax, numChildren, credit, totalTax;\n\n2\n\n3 printf(\"Please enter your Adjusted Gross Income: \");\n\n4 scanf(\"%lf\", &income);\n\n5\n\n6 printf(\"How many children do you have?\");\n\n7 scanf(\"%d\", &numChildren);\n\n8\n\n9 if(income < 0 || numChildren < 0) {\n\n10 printf(\"Invalid inputs\");\n\n11 exit(1);\n\n12 }\n\nNext, 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.\n\n1 if(income <= 18150) {\n\n2 baseTax = income * .10;\n\n3 } else if(income <= 73800) {\n\n4 baseTax = 1815 + (income - 18150) * .15;\n\n5 } else if(income <= 148850) {\n\n6 ...\n\n7 } else {\n\n8 baseTax = 127962.50 + (income - 457600) * .396;\n\n9 }"

Related Ideas

How can conditional statements be used to build a life and taxes program? | ComputerScienceOne | Bifalgorithm | Bifalgorithm