VNSGU BCA Sem 2: Programming Skills (204) Practical Solutions - Set E¶
Paper Details
- Subject: Programming Skills Using C
- Subject Code: 204
- Set: E
- Semester: 2
- Month/Year: April 2024
- Max Marks: 25
- Time Recommendation: 2 Hours
- Paper: View Paper | Download PDF
Questions & Solutions¶
All questions are compulsory¶
Q1: Net Salary Calculation¶
Max Marks: 20
Write a python program to calculate net salary where net salary = basic salary + hra + da - pf. HRA is 10% of basic salary, DA is 50% of basic salary and PF is 20% of basic salary.
Hint
Calculate each component (HRA, DA, PF) as percentage of basic salary, then compute net salary using the formula.
Solution Approach¶
flowchart TD
start[Start] --> input[Input Basic Salary]
input --> calc_hra[HRA = 10% of Basic]
calc_hra --> calc_da[DA = 50% of Basic]
calc_da --> calc_pf[PF = 20% of Basic]
calc_pf --> calc_net[Net = Basic + HRA + DA - PF]
calc_net --> display[Display All Details]
display --> end[End]
Solution - Python
def calculate_net_salary(basic_salary):
"""
Calculate net salary with HRA, DA, and PF deductions.
Parameters:
basic_salary (float): The basic salary amount
Returns:
dict: Dictionary containing all salary components
"""
# Calculate components
hra = basic_salary * 0.10 # 10% of basic
da = basic_salary * 0.50 # 50% of basic
pf = basic_salary * 0.20 # 20% of basic
# Calculate net salary
gross_salary = basic_salary + hra + da
net_salary = gross_salary - pf
return {
'basic': basic_salary,
'hra': hra,
'da': da,
'pf': pf,
'gross': gross_salary,
'net': net_salary
}
def display_salary_slip(salary_details):
"""Display formatted salary slip."""
print("\n" + "=" * 45)
print(" SALARY SLIP")
print("=" * 45)
print(f" Basic Salary : Rs. {salary_details['basic']:>10.2f}")
print(f" HRA (10%) : Rs. {salary_details['hra']:>10.2f}")
print(f" DA (50%) : Rs. {salary_details['da']:>10.2f}")
print("-" * 45)
print(f" Gross Salary : Rs. {salary_details['gross']:>10.2f}")
print(f" PF (20%) : Rs. {salary_details['pf']:>10.2f}")
print("-" * 45)
print(f" NET SALARY : Rs. {salary_details['net']:>10.2f}")
print("=" * 45)
# Main program
if __name__ == "__main__":
try:
# Get input from user
basic = float(input("Enter Basic Salary: "))
if basic < 0:
print("Basic salary cannot be negative.")
else:
# Calculate salary
salary_info = calculate_net_salary(basic)
# Display result
display_salary_slip(salary_info)
# Also display formula verification
print(f"\nFormula Verification:")
print(f" Net = {salary_info['basic']:.2f} + {salary_info['hra']:.2f} + {salary_info['da']:.2f} - {salary_info['pf']:.2f}")
print(f" Net = Rs. {salary_info['net']:.2f}")
except ValueError:
print("Invalid input! Please enter a valid number.")
except Exception as e:
print(f"An error occurred: {e}")
Sample Output:
Enter Basic Salary: 50000
=============================================
SALARY SLIP
=============================================
Basic Salary : Rs. 50000.00
HRA (10%) : Rs. 5000.00
DA (50%) : Rs. 25000.00
---------------------------------------------
Gross Salary : Rs. 80000.00
PF (20%) : Rs. 10000.00
---------------------------------------------
NET SALARY : Rs. 70000.00
=============================================
Formula Verification:
Net = 50000.00 + 5000.00 + 25000.00 - 10000.00
Net = Rs. 70000.00
Alternative: Using Class/Object-Oriented Approach
class SalaryCalculator:
"""Class to handle salary calculations."""
HRA_PERCENT = 0.10
DA_PERCENT = 0.50
PF_PERCENT = 0.20
def __init__(self, basic_salary):
self.basic = basic_salary
self.hra = self.basic * self.HRA_PERCENT
self.da = self.basic * self.DA_PERCENT
self.pf = self.basic * self.PF_PERCENT
self.net = self.basic + self.hra + self.da - self.pf
def display(self):
print(f"\nSalary Breakdown:")
print(f" Basic: Rs. {self.basic:.2f}")
print(f" HRA: Rs. {self.hra:.2f}")
print(f" DA: Rs. {self.da:.2f}")
print(f" PF: Rs. {self.pf:.2f}")
print(f" Net: Rs. {self.net:.2f}")
# Main
if __name__ == "__main__":
basic = float(input("Enter Basic Salary: "))
calc = SalaryCalculator(basic)
calc.display()
Alternative: Using Lambda and Dictionary
# Compact version using lambda
calculate = lambda basic: {
'basic': basic,
'hra': basic * 0.10,
'da': basic * 0.50,
'pf': basic * 0.20,
'net': basic * (1 + 0.10 + 0.50 - 0.20)
}
# Main
basic = float(input("Enter Basic Salary: "))
result = calculate(basic)
print(f"\nNet Salary: Rs. {result['net']:.2f}")
Alternative: Input with Validation Loop
def get_valid_salary():
"""Get valid salary input from user."""
while True:
try:
salary = float(input("Enter Basic Salary: "))
if salary >= 0:
return salary
print("Salary must be positive. Try again.")
except ValueError:
print("Invalid input. Please enter a number.")
# Main
basic = get_valid_salary()
net = basic * 1.40 # 1 + 0.10 + 0.50 - 0.20 = 1.40
print(f"Net Salary: Rs. {net:.2f}")
Q2: Viva Preparation¶
Max Marks: 5
Potential Viva Questions
- Q: What is the formula for net salary in this problem?
- A: Net = Basic + HRA(10%) + DA(50%) - PF(20%) = Basic × 1.40
- Q: Why is PF subtracted while HRA and DA are added?
- A: HRA and DA are allowances (added), PF is a deduction (subtracted).
- Q: What data type should be used for salary?
- A: float to handle decimal amounts like 50000.50
- Q: What is the difference between gross and net salary?
- A: Gross = Basic + Allowances, Net = Gross - Deductions
- Q: How do you format output to show 2 decimal places?
- A: Use f-strings with :.2f format specifier.
Common Pitfalls
- Calculation Order: HRA and DA are percentages of basic, not of gross.
- PF Deduction: Don't forget to subtract PF from gross.
- Input Type: Use float() not int() to accept decimal salaries.
- Negative Check: Always validate that salary is not negative.
Quick Navigation¶
Related Solutions¶
| Set | Link |
|---|---|
| Set A | Solutions |
| Set B | Solutions |
| Set C | Solutions |
| Set D | Solutions |
| Set E | Current Page |
| Set F | Solutions |
Last Updated: April 2024