#include // THIS IS MORE CHALLENGING // // Canada uses a progressive income-tax system. This means // that a higher tax rate applies only to the portion of // taxable income that falls within a higher tax bracket. // // For this exercise, we will use the 2021 Canadian federal // income-tax brackets: // // Taxable income Tax rate // // First $49,020 15% // From $49,020 to $98,040 20.5% // From $98,040 to $151,978 26% // From $151,978 to $216,511 29% // Above $216,511 33% // // These are the 2021 federal tax brackets. Actual income-tax // calculations also involve credits, deductions, provincial // or territorial taxes and other considerations. This // exercise considers only the federal bracket calculation. // // The important point is that reaching a higher tax bracket // does NOT cause all of your income to be taxed at the // higher rate. // // For example, someone with $50,000 of taxable income pays // // 15% on the first $49,020, // 20.5% on the remaining $980. // // Someone with $100,000 of taxable income pays // // 15% on the first $49,020, // 20.5% on the next $49,020, // 26% on the remaining $1,960. // // Someone with $200,000 of taxable income pays // // 15% on the first $49,020, // 20.5% on the next $49,020, // 26% on the next $53,938, // 29% on the remaining $48,022. // // The widths of the first four tax brackets are therefore: // // $0 to $49,020: $49,020 // $49,020 to $98,040: $49,020 // $98,040 to $151,978: $53,938 // $151,978 to $216,511: $64,533 // // Everything above $216,511 is taxed at 33%. // // Because we are using integer arithmetic, calculate // percentages as follows: // // 15% of amount: (amount*15)/100 // 20.5% of amount: (amount*205)/1000 // 26% of amount: (amount*26)/100 // 29% of amount: (amount*29)/100 // 33% of amount: (amount*33)/100 // // Integer division means that any fractional dollar of tax // calculated for a bracket will be discarded in this // simplified exercise. // // Write a program that asks the user for a non-negative // integer taxable income and calculates the federal income // tax according to these brackets. // // If the user enters a negative income, immediately // terminate the program by returning 0 from main(). // Function declarations int main(); // Function definitions int main() { int income{}; std::cout << "Enter your taxable income: "; std::cin >> income; if ( income < 0 ) { return 0; } // Calculate the federal income tax here. int income_tax{}; std::cout << "You owe $" << income_tax << " in federal income tax." << std::endl; return 0; } // Hint: // // One approach is to keep track of the amount of income // that has not yet been taxed. // // Begin with the first bracket. // // If all the remaining income fits within that bracket, // tax all of it at that rate. // // Otherwise: // // 1. tax the entire bracket, // 2. subtract the width of that bracket from the // remaining income, and // 3. continue with the next bracket. // // For example, suppose the remaining income is $100,000. // // After taxing the first $49,020, the amount remaining is // // 100000 - 49020 = 50980. // // After taxing the next $49,020, the amount remaining is // // 50980 - 49020 = 1960. // // That final $1,960 is then taxed at 26%. // Here is one solution: /* int main() { int income{}; std::cout << "Enter your taxable income: "; std::cin >> income; if ( income < 0 ) { return 0; } int income_tax{}; // Tax the first $49,020 at 15%. if ( income <= 49020 ) { income_tax += (income*15)/100; } else { income_tax += (49020*15)/100; income -= 49020; // Tax the next $49,020 at 20.5%. if ( income <= 49020 ) { income_tax += (income*205)/1000; } else { income_tax += (49020*205)/1000; income -= 49020; // Tax the next $53,938 at 26%. if ( income <= 53938 ) { income_tax += (income*26)/100; } else { income_tax += (53938*26)/100; income -= 53938; // Tax the next $64,533 at 29%. if ( income <= 64533 ) { income_tax += (income*29)/100; } else { income_tax += (64533*29)/100; income -= 64533; // Tax all remaining income at 33%. income_tax += (income*33)/100; } } } } std::cout << "You owe $" << income_tax << " in federal income tax." << std::endl; return 0; } */ // An alternative approach: // // Instead of nesting the conditional statements, we can // process one tax bracket at a time. // // After processing each bracket, subtract its width from // 'income'. If there is still income remaining, process // the next bracket. // // This produces code with less nesting: /* int main() { int income{}; std::cout << "Enter your taxable income: "; std::cin >> income; if ( income < 0 ) { return 0; } int income_tax{}; // Tax up to the first $49,020 at 15%. if ( income <= 49020 ) { income_tax += (income*15)/100; } else { income_tax += (49020*15)/100; } income -= 49020; // If income remains, tax up to the next $49,020 // at 20.5%. if ( income > 0 ) { if ( income <= 49020 ) { income_tax += (income*205)/1000; } else { income_tax += (49020*205)/1000; } income -= 49020; } // If income remains, tax up to the next $53,938 // at 26%. if ( income > 0 ) { if ( income <= 53938 ) { income_tax += (income*26)/100; } else { income_tax += (53938*26)/100; } income -= 53938; } // If income remains, tax up to the next $64,533 // at 29%. if ( income > 0 ) { if ( income <= 64533 ) { income_tax += (income*29)/100; } else { income_tax += (64533*29)/100; } income -= 64533; } // Any remaining income is taxed at 33%. if ( income > 0 ) { income_tax += (income*33)/100; } std::cout << "You owe $" << income_tax << " in federal income tax." << std::endl; return 0; } */