Calculate Tax Based on Salary Input in C++

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 have no tax.
- Salaries from $10,001 to $20,000 are taxed at 10%.
- Salaries from $20,001 to $40,000 are taxed at 20%.
- Salaries above $40,000 are taxed at 30%.

This exercise reinforces the use of conditional statements and arithmetic operations to implement a practical financial rule set.

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

 Copy 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

Share this C++ Exercise


More C++ Programming Exercises of Advanced Conditional Structures in C++

Explore our set of C++ Programming Exercises! Specifically designed for beginners, these exercises will help you develop a solid understanding of the basics of C++. From variables and data types to control structures and simple functions, each exercise is crafted to challenge you incrementally as you build confidence in coding in C++.

  • Dice Roll Simulation and Even-Odd Determination in C++

    In this C++ exercise, you will create a program that simulates the roll of a standard six-sided die. The result of the roll is randomly generated using the C++ random number facili...

  • Course Grading System Using Letter Grades in C++

    In this C++ exercise, you will create a simple program that simulates a letter-based grading system for a course. The program will take a student's numeric grade as input and conve...

  • Check if Two Strings Are Anagrams in C++

    In this C++ exercise, you will develop a program that checks whether two given strings are anagrams of each other. An anagram is formed by rearranging the letters of one word to pr...

  • Calculate Ticket Price Based on User's Age in C++

    In this C++ exercise, you will create a program that calculates the price of a ticket based on the age of the user. Different age groups are often charged different prices for even...

  • ATM Simulator with Multiple Operations in C++

    In this C++ exercise, you will create a simple simulation of an ATM (Automated Teller Machine). The program will allow the user to perform basic banking operations such as checking...

  • Guess the Number Game (1-100) in C++

    In this exercise, you will implement a program that simulates a simple number guessing game. The program will generate a random number between 1 and 100, and the player will attemp...