Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

C PROGRAM At the end of each month, a company processes payroll for its employee

ID: 3862189 • Letter: C

Question

C PROGRAM

At the end of each month, a company processes payroll for its employees. The company would like to calculate the employees’ salaries after taxes have been deducted. The data for all employees is stored in a text file. The company would like to generate a report that shows all the information for payroll.

Write a C program that reads employees’ numbers and monthly salaries from a text file. The program should output each employee's number, salary, taxes, and gross pay (salary after taxes). Output a summary for the total in each category. See the sample output below. The tax rate is based on the following scale:

0% for salaries below $1,000.

10% for salaries between $1,000 and $2,000.

15% for salaries of $2,000 or more.

Each line in the file contains an employee's number followed by his/her salary. Output the total salaries, total taxes, and total gross pay (use three variables to accumulate each total).

Create a data file called "payroll.txt" and include all employee data as shown in the sample data file below.

Requirements:

Write a function that calculates and returns the taxes

          taxes = calculate_taxes(salary);

Write a function that outputs a single salary line to the output file

          output_salary(emp_number, salary, taxes);

Write a function that outputs the summary line to the output file

          output_summary(total_salaries, total_taxes);

Use a field width in your output to line up the columns: 7 for employee number and 15 for all others (left justified).

Explanation / Answer

Here is the C code for you:

#include <stdio.h>
double calculate_taxes(double salary)
{
    double taxes;
    if(salary < 1000)
        taxes = 0.0;
    else if(salary < 2000)
       taxes *= 0.1;
    else
       taxes *= 0.15;  
    return taxes;  
}
void output_salary(int emp_number, double salary, double taxes)
{
    printf("%7d %15.2f %15.2f %15.2f ", emp_number, salary, taxes, salary-taxes);
}
void output_summary(double total_salaries, double total_taxes)
{
    printf("Total Salaries: %15.2f ", total_salaries);
    printf("Total Taxes: %15.2f ", total_taxes);
    printf("Total Gross pay: %15.2f ", total_salaries - total_taxes);
}
int main()
{
    int number;
    double salary, taxes, total_salaries = 0.0, total_taxes = 0.0;
    FILE *fp = fopen("payroll.txt", "r");
    while(!feof(fp))
    {
       fscanf(fp, "%d%lf", &number, &salary);
       taxes = calculate_taxes(salary);      
       output_salary(number, salary, taxes);
       total_salaries += salary;
       total_taxes += taxes;  
    }
    output_summary(total_salaries, total_taxes);
}