1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
#include <iostream> using namespace std; // Function to calculate the gross salary using pass-by-value double calculateGrossSalaryByValue(double basicSalary) { double hra, da, grossSalary; if (basicSalary <= 10000) { hra = 0.20 * basicSalary; // 20% HRA da = 0.80 * basicSalary; // 80% DA } else if (basicSalary <= 20000) { hra = 0.25 * basicSalary; // 25% HRA da = 0.90 * basicSalary; // 90% DA } else { hra = 0.30 * basicSalary; // 30% HRA da = 0.95 * basicSalary; // 95% DA } grossSalary = basicSalary + hra + da; return grossSalary; } // Function to calculate the gross salary using pass-by-reference void calculateGrossSalaryByReference(double basicSalary, double &grossSalary) { double hra, da; if (basicSalary <= 10000) { hra = 0.20 * basicSalary; // 20% HRA da = 0.80 * basicSalary; // 80% DA } else if (basicSalary <= 20000) { hra = 0.25 * basicSalary; // 25% HRA da = 0.90 * basicSalary; // 90% DA } else { hra = 0.30 * basicSalary; // 30% HRA da = 0.95 * basicSalary; // 95% DA } grossSalary = basicSalary + hra + da; } int main() { double basicSalary, grossSalaryByValue, grossSalaryByReference; // Input the basic salary cout << "Enter the basic salary of the employee: "; cin >> basicSalary; // Calculate gross salary using pass-by-value grossSalaryByValue = calculateGrossSalaryByValue(basicSalary); cout << "Gross Salary calculated using pass-by-value: $" << grossSalaryByValue << endl; // Calculate gross salary using pass-by-reference calculateGrossSalaryByReference(basicSalary, grossSalaryByReference); cout << "Gross Salary calculated using pass-by-reference: $" << grossSalaryByReference << endl; return 0; } |
Explanation:
- Pass-by-Value (
calculateGrossSalaryByValue
):- Function Definition:
double calculateGrossSalaryByValue(double basicSalary)
- This function receives a copy of the
basicSalary
. - It calculates the HRA (House Rent Allowance) and DA (Dearness Allowance) based on the salary brackets:
<= 10000
: HRA = 20%, DA = 80%<= 20000
: HRA = 25%, DA = 90%> 20000
: HRA = 30%, DA = 95%
- It then computes the gross salary and returns it.
- Function Definition:
- Pass-by-Reference (
calculateGrossSalaryByReference
):- Function Definition:
void calculateGrossSalaryByReference(double basicSalary, double &grossSalary)
- This function receives the
basicSalary
and a reference togrossSalary
. - It calculates the HRA and DA similarly but stores the result directly in the
grossSalary
reference. - The
grossSalary
variable in themain
function is updated with the computed value.
- Function Definition:
- Main Function (
main
):- The program prompts the user to input the basic salary.
- It calculates the gross salary using both methods and prints the results.
Example Outputs:
Example 1:
Input:
- Basic Salary:
12000
Output:
1 2 3 |
Enter the basic salary of the employee: 12000 Gross Salary calculated using pass-by-value: $21600 Gross Salary calculated using pass-by-reference: $21600 |
Example 2:
Input:
- Basic Salary:
25000
Output:
1 2 3 |
Enter the basic salary of the employee: 25000 Gross Salary calculated using pass-by-value: $48750 Gross Salary calculated using pass-by-reference: $48750 |
Example 3:
Input:
- Basic Salary:
9000
Output:
1 2 3 |
Enter the basic salary of the employee: 9000 Gross Salary calculated using pass-by-value: $16200 Gross Salary calculated using pass-by-reference: $16200 |