QUESTION #016

Calculate Gross Salary of Employee

Easy

📋 Problem Statement

Given the basic salary of an employee, calculate its gross salary according to the following conditions:

Basic Salary Range HRA (House Rent Allowance) DA (Dearness Allowance)
Basic Salary ≤ 10000 20% of basic 80% of basic
Basic Salary ≤ 20000 25% of basic 90% of basic
Basic Salary > 20000 30% of basic 95% of basic

Formula: Gross Salary = Basic Salary + HRA + DA

📥 Input Format

Enter an integer indicating the basic salary as input

📤 Output Format

Print the output as floating point value (2 decimal places) indicating the gross salary
Format: Rs.XXXXX.XX

💡 Examples

Example 1:
Input: 10000
Output: Rs.20000.00
Calculation:
Basic = 10000 (≤ 10000)
HRA = 10000 × 0.2 = 2000
DA = 10000 × 0.8 = 8000
Gross = 10000 + 2000 + 8000 = 20000.00
Example 2:
Input: 35000
Output: Rs.78750.00
Calculation:
Basic = 35000 (> 20000)
HRA = 35000 × 0.3 = 10500
DA = 35000 × 0.95 = 33250
Gross = 35000 + 10500 + 33250 = 78750.00
Example 3:
Input: 25468
Output: Rs.57303.00
Calculation:
Basic = 25468 (> 20000)
HRA = 25468 × 0.3 = 7640.4
DA = 25468 × 0.95 = 24194.6
Gross = 25468 + 7640.4 + 24194.6 = 57303.00

⚠️ Constraints

-10^9 <= INPUT <= 10^9

✅ Solution

#include <iostream>
#include <iomanip>

using namespace std;

int main() {
    long long basic;
    long double gross;
    cin >> basic;
    
    if (basic <= 10000)
        gross = basic + basic * 0.2 + basic * 0.8;
    else if (basic <= 20000 && basic > 10000)
        gross = basic + basic * 0.25 + basic * 0.9;
    else if (basic > 20000)
        gross = basic + basic * 0.3 + basic * 0.95;
    
    cout << fixed << setprecision(2) << "Rs." << gross;
    
    return 0;
}

🔑 Key Concepts

  • Else-if Ladder: Multiple mutually exclusive conditions for different salary ranges
  • Long Long Data Type: Used for basic salary to handle large input values up to 10^9
  • Long Double: For gross salary to maintain precision in percentage calculations
  • iomanip Library: fixed and setprecision(2) for 2 decimal places
  • Percentage Calculations: Converting percentages to decimals (20% = 0.2, 80% = 0.8)
  • Real-world Application: Salary calculation systems in payroll management

⚠️ Common Mistakes to Avoid

  • Integer Division: Don't use int for calculations involving percentages - precision will be lost
  • Missing setprecision: Without it, output won't have exactly 2 decimal places
  • Wrong Condition Order: Check smaller ranges first, then larger ones
  • Redundant Conditions: The && basic > 10000 in second condition is actually redundant due to else-if logic
  • Forgetting 'Rs.' Prefix: Output format specifically requires "Rs." before the amount

🎯 What You'll Learn

  • How to structure else-if ladders for range-based conditions
  • Working with floating-point precision in financial calculations
  • Choosing appropriate data types for large numbers and precise decimals
  • Using iomanip library for formatted output
  • Applying percentage calculations in real-world scenarios
  • Understanding payroll calculation logic