[go: up one dir, main page]

0% found this document useful (0 votes)
86 views13 pages

CSM 157 PROGRAMMING-Week6 2

The document presents an algorithm to calculate employee payroll including deductions. It is generalized to work for any number of employees by taking the number of employees as input. For each employee, it gets the hours worked and number of children as input. It then calculates the gross pay based on regular and overtime hours and rates. It calculates deductions for income tax, health levy, district tax, and education fund based on the gross pay. Finally, it displays the gross pay, each deduction, and net pay for the employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views13 pages

CSM 157 PROGRAMMING-Week6 2

The document presents an algorithm to calculate employee payroll including deductions. It is generalized to work for any number of employees by taking the number of employees as input. For each employee, it gets the hours worked and number of children as input. It then calculates the gross pay based on regular and overtime hours and rates. It calculates deductions for income tax, health levy, district tax, and education fund based on the gross pay. Finally, it displays the gross pay, each deduction, and net pay for the employee.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Problem 1: An algorithm to find average of two

numbers

INPUT FirstNumber
INPUT SecondNumber
Sum = FirstNumber + SecondNumber
Average = Sum/2
PRINT Average
Problem 2: An algorithm to find average of three
numbers

INPUT FirstNumber
INPUT SecondNumber
INPUT ThirdNumber
Sum = FirstNumber + SecondNumber+ThirdNumber
Average = Sum/3
PRINT Average
Problem 3: An algorithm to find average of four
numbers

INPUT FirstNumber
INPUT SecondNumber
INPUT ThirdNumber
INPUT FourthNumber
Sum = FirstNumber + SecondNumber+ThirdNumber+FourthNumber
Average = Sum/4
PRINT Average
If we look at solutions 1 to 3 careful, we see that
anytime the number of numbers to averaged
increases there is a corresponding increase in the
number of variables. The problem with the solutions
are that:
- Each of them is not a generalized solution
- We can use a single variable for all inputs

This will result in the following solution:


INPUT Number to get first number
Sum = Sum + Number to add first number
INPUT Number to get second number
Sum = Sum + Number to add second number
INPUT Number to get third number
Sum = Sum + Number to add third number
INPUT Number to get fourth number
Sum = Sum + Number to add fourth number
Average = Sum/4
PRINT Average
We can see from the solution above that the statements
INPUT Number
Sum = Sum + Number
have been repeated 4 times, 4 representing the number of numbers. We
can therefore use a FOR loop for the solution as follows:
Sum = 0
FOR i = 1 TO 4
INPUT Number
Sum = Sum + Number
ENDFOR
Average = Sum / 4
Print Average
Even though the use of the FOR loop has improved the solution, it is still
not generalized. We can generalized it if we represent the number of the
numbers to averaged by a variable, say NoOfNumbers. Then our solution
can be modified as follows:
Sum = 0
INPUT NoOfNumbers
FOR i = 1 TO NoOfNumbers
INPUT Number
Sum = Sum + Number
ENDFOR
Average = Sum / NoOfNumbers
Print Average
• By the modified solution above, the algorithm works for any number of
input and hence a generalised solution

• Whenever you write an algorithm, please note the following


- Minimise the number of variables used, if possible
- Make your program as short as possible. This ensure it does not take up
too much storage space
- Be conscious of the time your algorithm will take to run. Clients are not
interested in solutions that takes much time to run
Problem 4: Employees of a certain firm are paid on hourly basis. If an
employee works for not more than 40 hours a week, the hours worked
is considered regular and Overtime for hours worked beyond 40.
Regular hours are paid at 5 cedis per hour while the overtime rate is one
and half times the regular rate per hour. All employees are to pay 15% of
their gross pay as Income Tax, 2.5% as National Health Contribution
Levy, 1% as District Tax. Employees who have more than three children
are to pay 50 pesewas per child in excess of three towards Educational
Fund For All. Devise a computer solution that can be used to calculate
the necessary deductions as well as the Net Pay of employees. Your
display for each employee each deduction, net pay and the gross pay
with appropriate captions.
Let us pick the relevant information from the question and represent them
by some names
ITEM DESCRIPTION VARIABLE NAME
Number of Employees NoOfEmployees
Number of hours worked by an employee HoursWorked
Rate for payment of regular hours RegularHours
Rate of payment of Overtime hours OvertimeHours
Gross pay of an Employee GrossPay
Income tax of an Employee IncomeTax
National Health Insurance Levy of an employee NHIL
District Tax payable by employee DistrictTax
Number of children an Employee has NoOfChildren
Payment towards Educational Fund for All EduFund
All deductions Deductions
Net pay of an employee NetPay
Analysis of the problem
To solve the problem, we must do the following in the order presented:
(i) First, the number of employees should be provided as input
(ii) We then use a loop for items (iii) to (vi)
(iii) Get input on an employee, that is hours worked and the number of
children
(iv) Most deductions are dependent on the grass pay hence we calculate
the gross pay first
(v) We then calculate each of the deductions
(vi) We display the required output
Solution 4
DECLARE RegularRate, NoOfEmployees, HoursWorked,NoOfChildren, employee AS INTEGER
DECLARE GrossPay, OvertimeRate, IncomeTax,NHIL, DistrictTax,EduFund, NetPay AS DOUBLE
RegularRate = 5
OvertimeRate = 1.5*RegularRate
INPUT NoOfEmployees
FOR employee = 1 TO NoOfEmployees STEP 1
INPUT HoursWorked, NoOfChildren
IF HoursWorked <= 40 THEN
GrossPay = RegularRate * HoursWorked
ELSE
GrossPay = RegularRate * 40 + OvertimeRate* (HoursWorked-40)
ENDIF
IncomeTax = 0.15 * GrossPay
NHIL = 0.025 * GrossPay
DistrictTax = 0.01* GrossPay
IF NoOfChildren > 3 THEN
EduFund = 0.5 * (NoOfChildren - 3)
ELSE
EduFund = 0
ENDIF
NetPay = GrossPay – (IncomeTax+NHIL+DistrictTax+EduFund)
PRINT “The Gross pay is “, GrossPay
PRINT “The Income Tax is “, IncomeTax
PRINT “The National Health Levy is “, NHIL
PRINT “The District Tax is “, DistrictTax
PRINT “The Education Fund Contribution is ‘, EduFund
PRINT “The net pay is “, NetPay
ENDFOR

You might also like