Salary Calculation Using if Statement in C++

 


In this example, we will calculate the salary of an employee based on their basic salary and apply a bonus and deductions based on certain conditions using if statements.


1️⃣ Example: Salary Calculation with Bonus and Deductions

#include <iostream>
using namespace std;

int main() {
    double basicSalary, bonus = 0.0, deduction = 0.0, netSalary;

    // Taking basic salary input
    cout << "Enter basic salary: ";
    cin >> basicSalary;

    // Calculating bonus
    if (basicSalary >= 50000) {
        bonus = 0.2 * basicSalary;  // 20% bonus for salary >= 50,000
    } else if (basicSalary >= 30000) {
        bonus = 0.1 * basicSalary;  // 10% bonus for salary between 30,000 and 50,000
    } else {
        bonus = 0.05 * basicSalary;  // 5% bonus for salary below 30,000
    }

    // Calculating deductions
    if (basicSalary >= 70000) {
        deduction = 0.15 * basicSalary;  // 15% deduction for salary >= 70,000
    } else if (basicSalary >= 40000) {
        deduction = 0.1 * basicSalary;  // 10% deduction for salary between 40,000 and 70,000
    } else {
        deduction = 0.05 * basicSalary;  // 5% deduction for salary below 40,000
    }

    // Calculating net salary
    netSalary = basicSalary + bonus - deduction;

    // Displaying the results
    cout << "Basic Salary: " << basicSalary << endl;
    cout << "Bonus: " << bonus << endl;
    cout << "Deductions: " << deduction << endl;
    cout << "Net Salary: " << netSalary << endl;

    return 0;
}

Explanation:

  1. Bonus Calculation:

    • If the basic salary is greater than or equal to 50,000, a 20% bonus is given.
    • If the basic salary is between 30,000 and 50,000, a 10% bonus is given.
    • If the basic salary is below 30,000, a 5% bonus is given.
  2. Deductions Calculation:

    • If the basic salary is greater than or equal to 70,000, a 15% deduction is applied.
    • If the basic salary is between 40,000 and 70,000, a 10% deduction is applied.
    • If the basic salary is below 40,000, a 5% deduction is applied.
  3. Net Salary is calculated by adding the bonus and subtracting the deduction from the basic salary.


Output 1 (if basicSalary = 45000):

Enter basic salary: 45000
Basic Salary: 45000
Bonus: 4500
Deductions: 4500
Net Salary: 45000

Output 2 (if basicSalary = 75000):

Enter basic salary: 75000
Basic Salary: 75000
Bonus: 15000
Deductions: 11250
Net Salary: 78750

Output 3 (if basicSalary = 25000):

Enter basic salary: 25000
Basic Salary: 25000
Bonus: 1250
Deductions: 1250
Net Salary: 25000

2️⃣ Key Concepts in the Example

  • Bonus Calculation: The amount added to the basic salary based on certain conditions.
  • Deductions: The amount subtracted from the salary based on certain conditions.
  • Net Salary: The final salary after adding bonus and subtracting deductions.

3️⃣ Real-World Application:

  • This type of program can be used in payroll systems to automatically calculate an employee's net salary based on their basic salary and other factors such as bonus and deductions.
  • You can extend this example to consider taxes, overtime pay, or even performance-based bonuses using similar logic.

Would you like an extended example that includes additional factors like taxes, performance bonuses, or overtime?

Post a Comment

0 Comments