C++ Program: Calculate Income Tax Based on Salary
This C++ exercise involves calculating the tax to be paid based on a user's salary. The program prompts the user to enter their salary and then applies a set of tax rules to determine how much tax they need to pay. This type of logic is common in real-world applications such as payroll systems or financial software.
In this example, the tax rates are structured in tiers:
- Salaries up to $10,000: 0% tax.
- Salaries $10,001 to $20,000: 10% tax.
- Salaries $20,001 to $40,000: 20% tax.
- Salaries above $40,000: 30% tax.
How it works
Progressive Tiers
We use a structured if-else if ladder to segment the income into different taxable brackets correctly.
Arithmetic Logic
The program calculates cumulative tax by adding fixed amounts from lower tiers to the percentage of the current tier.
Pro Tip: Variable Precision
Always use float or double for currency. Using int would cause the program to truncate decimals, leading to incorrect tax totals.
Objectives
- 1. Input salary as a floating-point number.
- 2. Use if/else if for tiered tax brackets.
- 3. Apply 10%, 20%, and 30% rates to the corresponding amounts.
- 4. Display the total tax calculated.
Example C++ Code
#include <iostream>
using namespace std;
int main() {
float salary;
float tax = 0.0;
cout << "Enter your salary: ";
cin >> salary;
if (salary <= 10000) {
tax = 0;
} else if (salary <= 20000) {
tax = (salary - 10000) * 0.10;
} else if (salary <= 40000) {
tax = (10000 * 0.10) + (salary - 20000) * 0.20;
} else {
tax = (10000 * 0.10) + (20000 * 0.20) + (salary - 40000) * 0.30;
}
cout << "The tax to be paid is: $" << tax << endl;
return 0;
}
Console Output:
The tax to be paid is: $7000