This blog is under construction

Tuesday, 23 July 2013

C program to calculate employee salary using structures

Write a C program to calculate employee salary using structures.


  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>

  /* structure to store employee salary details */
  struct employee {
        int empId;
        char name[32];
        int basic, hra, da, ma;
        int pf, insurance;
        float gross, net;
  };

  /* prints payslip for the requested employee */
  void printSalary(struct employee e1) {
        printf("Salary Slip of %s:\n", e1.name);
        printf("Employee ID: %d\n", e1.empId);
        printf("Basic Salary: %d\n", e1.basic);
        printf("House Rent Allowance: %d\n", e1.hra);
        printf("Dearness Allowance: %d\n", e1.da);
        printf("Medical Allowance: %d\n", e1.ma);
        printf("Gross Salary: %.2f Rupees\n", e1.gross);

        printf("\nDeductions: \n");
        printf("Provident fund: %d\n", e1.pf);
        printf("Insurance: %d\n", e1.insurance);
        printf("\nNet Salary: %.2f Rupees\n\n", e1.net);
        return;
  }

  int main() {
        int i, ch, num, flag, empID;
        struct employee *e1;

        /* get the number of employees from the user  */
        printf("Enter the number of employees:");
        scanf("%d", &num);

        /* dynamically allocate memory to store employee salary details */
        e1 = (struct employee *)malloc(sizeof(struct employee) * num);

        /* get the employee salary details from the customer */
        printf("Enter your input for every employee:\n");
        for (i = 0; i < num; i++) {
                printf("Employee ID:");
                scanf("%d", &(e1[i].empId));
                getchar();
                printf("Employee Name:");
                fgets(e1[i].name, 32, stdin);
                e1[i].name[strlen(e1[i].name) - 1] = '\0';
                printf("Basic Salary, HRA:");
                scanf("%d%d", &(e1[i].basic), &(e1[i].hra));
                printf("DA, Medical Allowance:");
                scanf("%d%d", &(e1[i].da), &(e1[i].ma));
                printf("PF and Insurance:");
                scanf("%d%d", &(e1[i].pf), &(e1[i].insurance));
                printf("\n");

        }

        /* gross and net salary calculation */
        for (i = 0; i < num; i++) {
                e1[i].gross = e1[i].basic +
                        (e1[i].hra * e1[i].basic) / 100 +
                                (e1[i].da * e1[i].basic) / 100 +
                                        (e1[i].ma * e1[i].basic) / 100;
                e1[i].net = e1[i].gross - (e1[i].pf + e1[i].insurance);
        }

        /* printing payslip for the given employee ID */
        while (1) {
                printf("Enter employee ID to get payslip:");
                scanf("%d", &empID);
                flag = 0;
                for (i = 0; i < num; i++) {
                        if (empID == e1[i].empId) {
                                printSalary(e1[i]);
                                flag = 1;
                        }
                }

                if (!flag) {
                        printf("No Record Found!!\n");
                }

                printf("Do You Want To Continue(1/0):");
                scanf("%d", &ch);

                if (!ch) {
                        break;
                }
        }
        return 0;
  }



  Output:
  jp@jp-VirtualBox:~/$ ./a.out
  Enter the number of employees:3
  Enter your input for every employee:
  Employee ID:101
  Employee Name:Sam
  Basic Salary, HRA:5000 500
  DA, Medical Allowance:300 500
  PF and Insurance:1000 400

  Employee ID:102
  Employee Name:Ram
  Basic Salary, HRA:3000 200
  DA, Medical Allowance:300 500
  PF and Insurance:800 200

  Employee ID:103
  Employee Name:Raj
  Basic Salary, HRA:4000 300
  DA, Medical Allowance:300 500
  PF and Insurance:1000 200

  Enter employee ID to get payslip:102
  Salary Slip of Ram:
  Employee ID: 102
  Basic Salary: 3000
  House Rent Allowance: 200
  Dearness Allowance: 300
  Medical Allowance: 500
  Gross Salary: 33000.00 Rupees

  Deductions: 
  Provident fund: 800
  Insurance: 200
  Net Salary: 32000.00 Rupees
  Do You Want To Continue(1/0):1

  Enter employee ID to get payslip:101
  Salary Slip of Sam:
  Employee ID: 101
  Basic Salary: 5000
  House Rent Allowance: 500
  Dearness Allowance: 300
  Medical Allowance: 500
  Gross Salary: 70000.00 Rupees

  Deductions: 
  Provident fund: 1000
  Insurance: 400
  Net Salary: 68600.00 Rupees

  Do You Want To Continue(1/0):1
  Enter employee ID to get payslip:4
  No Record Found!!

  Do You Want To Continue(1/0):0


3 comments:

  1. How convert this net salary in to words

    ReplyDelete
  2. Thank you for your good blog post. If you interested in It workers salaries, check my blog

    ReplyDelete
  3. This comment has been removed by the author.

    ReplyDelete