Open In App

Program to calculate gross salary of a person

Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer basic and a character grade which denotes the basic salary and grade of a person respectively, the task is to find the gross salary of the person.

Gross Salary: The final salary computed after the additions of DA, HRA and other allowances. The formula for Gross Salary is defined as below:

Gross Salary = Basic + HRA + DA + Allow – PF
Here, HRA = 20% of Basic 
DA = 50% of basic 
Allow = 1700 if grade = ‘A’ 
Allow = 1500 if grade = ‘B’ 
Allow = 1300 if grade = ‘C’ 
PF = 11% of basic

Examples:

Input: basic = 10000, grade = ‘A’
Output: 17600

Input: basic = 4567, grade = ‘B’
Output: 8762

Approach: The idea is to find the allowance on the basis of the grade and then compute the HRA, DA, and PF on the basis of the basic salary. Below is the illustration of the computation of HRA, DA, and PF:

  • HRA: House Rent Allowance is 20% of the basic salary:

 
 

HRA = Basic Salary * 0.20

 

  • DA: Daily Allowance is the 50% of the basic salary:

 
 

DA = Basic Salary * 0.5

 

  • PF: Provident Fund is the 11% of the basic salary.

 
 

PF = Basic Salary * 0.11

 

Below is the implementation of the above approach:

C++




// C++ Program to implement
// the above approach
 
#include <bits/stdc++.h>
using namespace std;
 
// Function to calculate the
// salary of the person
int computeSalary(int basic,
                char grade)
{
    int allowance;
    double hra, da, pf;
 
    hra = 0.2 * basic;
    da = 0.5 * basic;
    pf = 0.11 * basic;
 
    // Condition to compute the
    // allowance for the person
    if (grade == 'A') {
        allowance = 1700;
    }
    else if (grade == 'B') {
        allowance = 1500;
    }
    else {
        allowance = 1300;
    }
    int gross;
 
    // Calculate gross salary
    gross = round(basic + hra + da + allowance - pf);
    return gross;
}
 
// Driver Code
int main()
{
    int basic = 10000;
    char grade = 'A';
 
    cout << computeSalary(basic, grade);
}


Java




// Java program to implement
// the above approach
import java.util.*;
import java.lang.*;
 
class GFG{
 
// Function to calculate the
// salary of the person
static int computeSalary(int basic,
                        char grade)
{
    double allowance;
    double hra, da, pf;
     
    hra = 0.2 * basic;
    da = 0.5 * basic;
    pf = 0.11 * basic;
     
    // Condition to compute the
    // allowance for the person
    if (grade == 'A')
    {
        allowance = 1700.0;
    }
    else if (grade == 'B')
    {
        allowance = 1500.0;
    }
    else
    {
        allowance = 1300.0;
    }
    double gross;
     
    // Calculate gross salary
    gross = Math.round(basic + hra + da +
                         allowance - pf);
                          
    return (int)gross;
}
 
// Driver Code
public static void main (String[] args)
{
    int basic = 10000;
    char grade = 'A';
     
    // Function call
    System.out.println(computeSalary(basic, grade));
}
}
 
// This code is contributed by jana_sayantan


Python3




# Python3 program to implement
# the above approach
 
# Function to calculate the
# salary of the person
def computeSalary( basic, grade):
     
    hra = 0.2 * basic
    da = 0.5 * basic
    pf = 0.11 * basic
     
    # Condition to compute the
    # allowance for the person
    if grade == 'A':
        allowance = 1700.0
    elif grade == 'B':
        allowance = 1500.0
    else:
        allowance = 1300.0;
     
    gross = round(basic + hra + da +
                    allowance - pf)
                     
    return gross
 
# Driver code
if __name__ == '__main__':
     
    basic = 10000
    grade = 'A'
     
    # Function call
    print(computeSalary(basic, grade));
 
# This code is contributed by jana_sayantan


C#




// C# program to implement
// the above approach
using System;
 
class GFG{
 
// Function to calculate the
// salary of the person
static int computeSalary(int basic,
                        char grade)
{
    double allowance;
    double hra, da, pf;
     
    hra = 0.2 * basic;
    da = 0.5 * basic;
    pf = 0.11 * basic;
     
    // Condition to compute the
    // allowance for the person
    if (grade == 'A')
    {
        allowance = 1700.0;
    }
    else if (grade == 'B')
    {
        allowance = 1500.0;
    }
    else
    {
        allowance = 1300.0;
    }
    double gross;
     
    // Calculate gross salary
    gross = Math.Round(basic + hra + da +
                         allowance - pf);
                          
    return (int)gross;
}
 
// Driver Code
public static void Main (String[] args)
{
    int basic = 10000;
    char grade = 'A';
     
    // Function call
    Console.WriteLine(computeSalary(basic, grade));
}
}
 
// This code is contributed by jana_sayantan


Javascript




<script>
 
// JavaScript program for
// the above approach
 
// Function to calculate the
// salary of the person
function computeSalary(basic,
                        grade)
{
    let allowance;
    let hra, da, pf;
       
    hra = 0.2 * basic;
    da = 0.5 * basic;
    pf = 0.11 * basic;
       
    // Condition to compute the
    // allowance for the person
    if (grade == 'A')
    {
        allowance = 1700.0;
    }
    else if (grade == 'B')
    {
        allowance = 1500.0;
    }
    else
    {
        allowance = 1300.0;
    }
    let gross;
       
    // Calculate gross salary
    gross = Math.round(basic + hra + da +
                         allowance - pf);
                            
    return gross;
}
 
// Driver code
    let basic = 10000;
    let grade = 'A';
       
    // Function call
    document.write(computeSalary(basic, grade));
    
   // This code is contributed by splevel62.
</script>


Output: 

17600

Time Complexity: O(1) 
Auxiliary Space: O(1)
 



Last Updated : 16 Apr, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads