You can use nested if statements to write a program for computing taxes.
OJ Point The United States federal personal income tax is calculated based on filing status and taxable income. There are four filing statuses: single filers, married filing jointly or qualified widow(er), married filing separately, and head of household. The tax rates vary every year. Table 3.2 shows the rates for 2009. If you are, say, single with a taxable income of $10,000, the first $8,350 is taxed at 10% and the other $1,650 is taxed at 15%, so, your total tax is $1,082.50.
You are to write a program to compute personal income tax. Your program should prompt the user to enter the filing status and taxable income and compute the tax. Enter 0 for single filers, 1 for married filing jointly or qualified widow(er), 2 for married filing separately, and 3 for head of household.
Your program computes the tax for the taxable income based on the filing status. The filing status can be determined using if statements outlined as follows:
if (status == 0)
{
// Compute tax for single filers
}
else if (status == 1)
{
// Compute tax for married filing jointly or qualifying widow(er)
}
else if (status == 2)
{
// Compute tax for married filing separately
}
else if (status == 3)
{
// Compute tax for head of household
}
else {
// Display wrong status
}
For each filing status there are six tax rates. Each rate is applied to a certain amount of taxable income. For example, of a taxable income of $400,000 for single filers, $8,350 is taxed at 10%, (33,950 – 8,350) at 15%, (82,250 – 33,950) at 25%, (171,550 – 82,250) at 28%, (372,950 – 171,550) at 33%, and (400,000 – 372,950) at 35%.
Listing 3.3 gives the solution to compute taxes for single filers. The complete solution is left as an exercise.
Listing 3.3 ComputeTax.cpp
1 #include <iostream>
2 using namespace std;
3
4 int main()
5 {
6 // Prompt the user to enter filing status
7 cout << “(0-single filer, 1-married jointly, “
8 << “or qualifying widow(er), ” << endl
9 << “2-married separately, 3-head of household)” << endl
10 << “Enter the filing status: “;
11
12 int status;
13 cin >> status;
14
15 // Prompt the user to enter taxable income
16 cout << “Enter the taxable income: “;
17 double income;
18 cin >> income;
19
20 // Compute tax
21 double tax = 0;
22
23 if (status == 0) // Compute tax for single filers
24 {
25 if (income <= 8350)
26 tax = income * 0.10;
27 else if (income <= 33950)
28 tax = 8350 * 0.10 + (income – 8350) * 0.15;
29 else if (income <= 82250)
30 tax = 8350 * 0.10 + (33950 – 8350) * 0.15 +
31 (income – 33950) * 0.25;
32 else if (income <= 171550)
33 tax = 8350 * 0.10 + (33950 – 8350) * 0.15 +
34 (82250 – 33950) * 0.25 + (income – 82250) * 0.28;
35 else if (income <= 372950)
36 tax = 8350 * 0.10 + (33950 – 8350) * 0.15 +
37 (82250 – 33950) * 0.25 + (171550 – 82250) * 0.28 +
38 (income – 171550) * 0.33;
39 else
40 tax = 8350 * 0.10 + (33950 – 8350) * 0.15 +
41 (82250 – 33950) * 0.25 + (171550 – 82250) * 0.28 +
42 (372950 – 171550) * 0.33 + (income – 372950) * 0.35;
43 }
44 else if (status == 1) // Compute tax for married file jointly
45 {
46 // Left as an exercise
47 }
48 else if (status == 2) // Compute tax for married separately
49 {
50 // Left as an exercise
51 }
52 else if (status == 3) // Compute tax for head of household
53 {
54 // Left as an exercise
55 }
56 else
57 {
58 cout << “Error: invalid status”;
59 return 0;
60 }
61
62 // Display the result
63 cout << “Tax is ” << static_cast<int>(tax * 100) / 100.0 << endl;
64
65 return 0;
66 }
The program receives the filing status and taxable income. The multi-way if-else statements (lines 23, 44, 48, 52, 56) check the filing status and compute the tax on which it is based.
To test a program, you should provide the input that covers all cases. For this program, your input should cover all statuses (0, 1, 2, 3). For each status, test the tax for each of the six brackets. So, there are a total of 24 cases.
Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.