There are certain tax ranges, for example, 0 - 10,000 gets taxed at 20%, 10,000-20,000 gets taxed at 25%, etc. So if your income was 15,000, the first 10,000 is taxed at 20%, and the remaining 5,000 is taxed at 25%. Rather than using a bunch of if statements, (ex. if (income <= 10,000){do this} if income <=20,000 && income >= 10,000){do this})
I would like to use a for loop to calculate income tax. The tax rates and ranges are stored in arrays. I have an idea how to do it, but can't quite get it right.
1 2 3 4 5 6 7
|
for (i=0; i < size_of_array; i++)
{
if (income <= array[i]) //if the income is less than the first element of the array, ie 10,000
{
tax = rate * income
}
else tax = (rate * bracket) + (income - bracket)*rate
|
I know how to do the calculation by hand, as I showed above, but I'm having a hard time converting the logic to c++