Python Program to Calculate Gross Salary of a Person
Last Updated :
27 Jul, 2023
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.
Python Program to Calculate Gross Salary of a 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:
Python3
def computeSalary( basic, grade):
hra = 0.2 * basic
da = 0.5 * basic
pf = 0.11 * basic
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
if __name__ = = '__main__' :
basic = 10000
grade = 'A'
print (computeSalary(basic, grade));
|
Time Complexity: O(1)
Auxiliary Space: O(1)
One approach that could be used in this situation is to use a dictionary to store the allowance amounts for each grade, rather than using a series of if-elif statements. This would allow for easier modification of the allowance amounts in the future, as the values could be easily accessed and changed in the dictionary.
For example, the code could be modified as follows:
Python3
allowance_amounts = { 'A' : 1700.0 , 'B' : 1500.0 , 'C' : 1300.0 }
def computeSalary(basic, grade):
hra = 0.2 * basic
da = 0.5 * basic
pf = 0.11 * basic
allowance = allowance_amounts[grade]
gross = round (basic + hra + da + allowance - pf)
return gross
if __name__ = = '__main__' :
basic = 10000
grade = 'A'
print (computeSalary(basic, grade))
|
Time complexity: O(1)
Auxiliary space O(1), as the dictionary is constant and does not depend on the input size.
Please refer complete article on Program to calculate gross salary of a person for more details!
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...