Tax Brackets
January 26, 2021
My W-2 form came in the mail a few days ago, so a task about taxes is appropriate. Given the following tax brackets:
income cap tax rate
$ 10,000 0%
$ 30,000 10%
$100,000 25%
more 40%
Calculate the amount of tax given an amount of income. The tax brackets work like this: If your income is less than $10,000, your tax is $0. If your income is between $10,000 and $30,000, your tax is 10% of your income in excess of $10,000. If your income is between $30,000 and $100,000, your tax is 10% of the $20,000 of income between $10,000 and $30,000 (or $2,000) plus 25% of your income over $30,000. If your income is over $100,000, your tax is 10% of the $20,000 of income between $10,000 and $30,000 (or $2,000), plus 25% of your income between $30,000 and $100,000 (or $17,500), plus 40% of your income over $100,000 (ouch!).
For example, if your income is $123,456, your tax is $2,000 plus $17,500 plus 40% of $23,456 (or $9,382.40), a total tax of $28,882.40, or 23.4% of your income.
Your task is to write a program that calculates the amount of tax due for a given amount of income. When you are finished, you are welcome to read or run a suggested solution, or to post your own solution or discuss the exercise in the comments below.
Perl
Forgot to say – the trick here is to only add the additional tax per bracket – so about 10K is 10%, above 30K is then (25-10)% = 15% and about 100K is then (40-25)% = 15% as well…
A solution in Racket:
Solution in use:
Here’s a solution in Python.
Output:
Solution in Java: