PROBLEM:
Write a program to calculate an employees wages. They are paid at a basic rate of £6.65 hour, but are taxed at a rate of 23% and pay a pension contribution of 4% and pay a union subscription of 1.25 per week.
Write a program to read in the hours worked and calculate the take home weekly wages of the employee.
SOLUTION:
The structure of the program will take the following form:-
#include <stdio.h> /*Declare constants */ main() { /*declare variables */ /*prompt user to enter number of hours worked and read in value */ /* Calculate wages */ /* output weekly wages */ }
Constants
We are told that the employee is paid 6.65 per hour is taxed at a rate of 23%, pays 4% pension contributions and a union fee of 1.25. We should make these values be constants - since they have been given to use. In addition, this makes it easier to change these values at a later date and if we use meaningful names will make the program easier to read. Therefore we will declare:-
#define hourly_rate 6.65 #define tax_rate 0.23 #define pension_rate 0.04 #define union_fee 1.25
I have converted the percentages to floating point values, since for example 23% = 23/100 * value.
Variables
We are obviously going to require a variable to store the number of hours worked, which we will assume can be a fractional number of hours and therefore use a floating point value. We will also use variables for the gross_pay, pension, tax, deductions and total_pay. All of which will be floating point values.
float hours, gross_pay, tax, pension, deductions, total_pay;
Input
We require to prompt the user to enter the number of hours worked and then read this into the variable hours .
printf("Enter the number of hours worked \n"); scanf("%f", &hours);
Calculation
There are a number of different ways this could be implemented. I have chosen here to do it in a number of different stages. Calculating initially the gross wages and then the deductions.
gross_pay = hours * hourly_rate; tax = gross_pay * tax_rate; pension = gross_pay * pension_rate; deductions = tax + pension + union_fee; total_pay = gross_pay - deductions;
Output
Finally we have to output the result. This can be achieved by the statement
printf("The total weekly pay is %0.2f\n", total_pay);
Note the use of the %0.2f for the output field. This ensures that the output is written to 2 decimal places (pounds and pence) as this is all that is required.
Total Program
#include <stdio.h> /*Declare constants */ #define hourly_rate 6.65 #define tax_rate 0.23 #define pension_rate 0.04 #define union_fee 1.25 main() { /*declare variables */ float hours, gross_pay, tax, pension, deductions, total_pay; /*prompt user to enter number of hours worked and read in value */ printf("Enter the number of hours worked \n"); scanf("%f", &hours); /* Calculate wages */ gross_pay = hours * hourly_rate; tax = gross_pay * tax_rate; pension = gross_pay * pension_rate; deductions = tax + pension + union_fee; total_pay = gross_pay - deductions; /* output weekly wages */ printf("The total weekly pay is %0.2f\n", total_pay); }
Back to Main Page for Lecture 2 |