C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

File Handling in C

Payroll Generation for employee

Employee Payroll System to include an automatic Payslip Generation feature — where the program can print a detailed monthly salary slip for each employee stored in the file.

C Program: Employee Payroll System with Payslip Generation

This program includes:

  • Adding, displaying, searching, updating, and deleting employee records.
  • Generating a formatted Payslip (salary breakdown) for a specific employee.

 

C

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

 

struct Employee {

    int id;

    char name[50];

    float basicSalary;

    float hra, da, gross, tax, net;

};

 

// Function to calculate gross and net salary

void calculateSalary(struct Employee *e) {

    e->hra = e->basicSalary * 0.20;   // 20% HRA

    e->da = e->basicSalary * 0.10;    // 10% DA

    e->gross = e->basicSalary + e->hra + e->da;

 

    // Simple tax logic

    if (e->gross > 50000)

        e->tax = e->gross * 0.10; // 10% tax

    else if (e->gross > 30000)

        e->tax = e->gross * 0.05; // 5% tax

    else

        e->tax = 0;

 

    e->net = e->gross - e->tax;

}

 

// Add new employee

void addEmployee() {

    FILE *fp;

    struct Employee e;

    fp = fopen("payroll.dat", "ab");

    if (!fp) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("\nEnter Employee ID: ");

    scanf("%d", &e.id);

    printf("Enter Name: ");

    scanf("%s", e.name);

    printf("Enter Basic Salary: ");

    scanf("%f", &e.basicSalary);

 

    calculateSalary(&e);

    fwrite(&e, sizeof(e), 1, fp);

    fclose(fp);

    printf("Employee added successfully!\n");

}

 

// Display all employee records

void displayEmployees() {

    FILE *fp;

    struct Employee e;

    fp = fopen("payroll.dat", "rb");

    if (!fp) {

        printf("No records found!\n");

        return;

    }

 

    printf("\n--- Employee Payroll Records ---\n");

    printf("%-10s %-15s %-12s %-10s %-10s %-10s %-10s\n",

           "ID", "Name", "Basic", "HRA", "DA", "Gross", "Net");

 

    while (fread(&e, sizeof(e), 1, fp)) {

        printf("%-10d %-15s %-12.2f %-10.2f %-10.2f %-10.2f %-10.2f\n",

               e.id, e.name, e.basicSalary, e.hra, e.da, e.gross, e.net);

    }

 

    fclose(fp);

}

 

// Generate payslip for specific employee

void generatePayslip() {

    FILE *fp;

    struct Employee e;

    int id, found = 0;

 

    fp = fopen("payroll.dat", "rb");

    if (!fp) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("\nEnter Employee ID to generate payslip: ");

    scanf("%d", &id);

 

    while (fread(&e, sizeof(e), 1, fp)) {

        if (e.id == id) {

            found = 1;

            printf("\n==============================================\n");

            printf("              EMPLOYEE PAYSLIP\n");

            printf("==============================================\n");

            printf("Employee ID     : %d\n", e.id);

            printf("Employee Name   : %s\n", e.name);

            printf("----------------------------------------------\n");

            printf("Basic Salary    : %.2f\n", e.basicSalary);

            printf("HRA (20%%)       : %.2f\n", e.hra);

            printf("DA (10%%)        : %.2f\n", e.da);

            printf("Gross Salary    : %.2f\n", e.gross);

            printf("Tax Deduction   : %.2f\n", e.tax);

            printf("----------------------------------------------\n");

            printf("Net Salary      : %.2f\n", e.net);

            printf("==============================================\n\n");

            break;

        }

    }

 

    if (!found)

        printf("Employee not found!\n");

 

    fclose(fp);

}

 

// Update employee salary

void updateEmployee() {

    FILE *fp;

    struct Employee e;

    int id, found = 0;

    long pos;

 

    fp = fopen("payroll.dat", "rb+");

    if (!fp) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("\nEnter Employee ID to update: ");

    scanf("%d", &id);

 

    while (fread(&e, sizeof(e), 1, fp)) {

        if (e.id == id) {

            found = 1;

            pos = ftell(fp) - sizeof(e);

 

            printf("Enter new Basic Salary: ");

            scanf("%f", &e.basicSalary);

            calculateSalary(&e);

 

            fseek(fp, pos, SEEK_SET);

            fwrite(&e, sizeof(e), 1, fp);

            printf("Employee record updated!\n");

            break;

        }

    }

 

    if (!found)

        printf("Employee not found!\n");

 

    fclose(fp);

}

 

// Delete employee

void deleteEmployee() {

    FILE *fp, *temp;

    struct Employee e;

    int id, found = 0;

 

    fp = fopen("payroll.dat", "rb");

    temp = fopen("temp.dat", "wb");

 

    if (!fp || !temp) {

        printf("Error opening file!\n");

        return;

    }

 

    printf("Enter Employee ID to delete: ");

    scanf("%d", &id);

 

    while (fread(&e, sizeof(e), 1, fp)) {

        if (e.id != id)

            fwrite(&e, sizeof(e), 1, temp);

        else

            found = 1;

    }

 

    fclose(fp);

    fclose(temp);

 

    remove("payroll.dat");

    rename("temp.dat", "payroll.dat");

 

    if (found)

        printf("Employee deleted successfully!\n");

    else

        printf("Employee not found!\n");

}

 

// Main Menu

int main() {

    int choice;

 

    do {

        printf("\n===== Employee Payroll System =====\n");

        printf("1. Add Employee\n");

        printf("2. Display All Employees\n");

        printf("3. Update Employee Salary\n");

        printf("4. Delete Employee\n");

        printf("5. Generate Payslip\n");

        printf("6. Exit\n");

        printf("Enter your choice: ");

        scanf("%d", &choice);

 

        switch (choice) {

            case 1: addEmployee(); break;

            case 2: displayEmployees(); break;

            case 3: updateEmployee(); break;

            case 4: deleteEmployee(); break;

            case 5: generatePayslip(); break;

            case 6: printf("Exiting...\n"); break;

            default: printf("Invalid choice!\n");

        }

    } while (choice != 6);

 

    return 0;

}

Output

 
OUTPUT :

==============================================
              EMPLOYEE PAYSLIP
==============================================
Employee ID     : 102
Employee Name   : Alice
----------------------------------------------
Basic Salary    : 45000.00
HRA (20%)       : 9000.00
DA (10%)        : 4500.00
Gross Salary    : 58500.00
Tax Deduction   : 5850.00
----------------------------------------------
Net Salary      : 52650.00
==============================================


How the Payslip Feature Works

When you select Option 5 (Generate Payslip):

  • It asks for an Employee ID.
  • Searches in the file.
  • Displays a formatted payslip with:
    • Basic salary
    • HRA (20%)
    • DA (10%)
    • Gross salary
    • Tax deduction (if applicable)
    • Net salary