Group
Advanced Conditional Structures in C++
Objective
1. Ask the user to input their salary as a floating-point number.
2. Apply a set of conditional tax rules based on the salary range.
3. Calculate and display the amount of tax owed.
Implement a program that calculates the tax payable based on salary.
Example C++ Exercise
Show C++ Code
#include <iostream> // Include the input/output library
using namespace std;
int main() {
// Declare a variable to store the salary
float salary;
// Declare a variable to store the tax to be paid
float tax = 0.0;
// Prompt the user to enter their salary
cout << "Enter your salary: ";
cin >> salary;
// Apply tax rules based on salary range
if (salary <= 10000) {
// No tax for salaries up to $10,000
tax = 0;
} else if (salary <= 20000) {
// 10% tax for salaries between $10,001 and $20,000
tax = (salary - 10000) * 0.10;
} else if (salary <= 40000) {
// 10% for the first $10,000 after $10,000, and 20% for the rest
tax = (10000 * 0.10) + (salary - 20000) * 0.20;
} else {
// 10% for $10,000, 20% for $20,000, and 30% for the amount above $40,000
tax = (10000 * 0.10) + (20000 * 0.20) + (salary - 40000) * 0.30;
}
// Display the calculated tax
cout << "The tax to be paid is: $" << tax << endl;
return 0; // End of program
}
Output
Enter your salary: 45000
The tax to be paid is: $7000