C Program for Employee Details using Structure

programming

Problem: Write a C program to read and print employee details using structure.

To store multiple employee details we will use an array of structures. Each element in the array will represent a single employee.

Each Structure i.e. Employee contains:

  • Name
  • Id
  • Salary

In our program, we will read the inputs for each employee from the user, and then output all employee details by iterating through the array using the ‘for’ loop.

C Program to Read and Print Employee Details

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    char name[30];
    int id;
    double salary;
} Employee;
 
int main()
{
    //number of employees
    int n=2;

    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n",i+1);

        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);

        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);

        //Salary
        printf("Salary: ");
        scanf("%lf",&employees[i].salary);

        //to consume extra '\n' input
        char ch = getchar();
 
        printf("\n");
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
        printf("Name \t: ");
        printf("%s \n",employees[i].name);
 
        printf("Id \t: ");
        printf("%d \n",employees[i].id);
 
        printf("Salary \t: ");
        printf("%.2lf \n",employees[i].salary);
 
        printf("\n");
    }
 
    return 0;
}

The output of the Program:

C Program for Employee Details using Structure

Calculate Salary of the Employee from Basic

In the above program, we are directly asking the user to input the salary of every employee.

In case we need to calculate the salary from the basic, we will have to add an extra variable (i.e. basic_salary) to the structure and calculate the net_salary using the formula:  net_salary = basic_salary + HRA + DA – PF

Where,

  • HRA is 10% of basic salary i.e., HRA=basic_salary*0.1;
  • DA is 5% of basic salary i.e., DA=basic_salary*0.05;
  • PF is 12% of basic salary i.e., PF=basic_salary*0.12;

Putting them in the formula: net_salary = basic_salary * (1+ 0.1 + 0.05 – 0.12).

If we modify the above program then the code will be:

#include <stdio.h>
#include <stdlib.h>
 
typedef struct{
    
    char name[30];
    int id;
    double basic_salary;
    double net_salary;
    
} Employee;
 
int main()
{
    //number of employees
    int n=2;
    
    //array to store structure values of all employees
    Employee employees[n];
 
    //Taking each employee detail as input
    printf("Enter %d Employee Details \n \n",n);
    for(int i=0; i<n; i++){
        printf("Employee %d:- \n", (i+1));
        
        //Name
        printf("Name: ");
        scanf("%[^\n]s",employees[i].name);
        
        //ID
        printf("Id: ");
        scanf("%d",&employees[i].id);
        
        //Salary
        printf("Basic Salary: ");
        scanf("%lf",&employees[i].basic_salary);
        
        //to consume extra '\n' input
        char ch = getchar();
        
        employees[i].net_salary = employees[i].basic_salary*1.03;
    }
 
    //Displaying Employee details
    printf("-------------- All Employees Details ---------------\n");
    for(int i=0; i<n; i++){
 
        printf("Name \t: ");
        printf("%s \n",employees[i].name);
 
        printf("Id \t: ");
        printf("%d \n",employees[i].id);
 
        printf("Salary \t: ");
        printf("%.2lf \n",employees[i].net_salary);
 
        printf("\n");
    }
 
    return 0;
}

If you didn’t understand any part of the code then do comment below.

30 thoughts on “C Program for Employee Details using Structure”

    1. They both are similar. The difference is the type of data in the array. When we declare int array[10], we mean to store integers into the array and in case of Employee array[10], we mean to store structure objects into the array.

    1. Any name you put at the end of the structure declaration along with typedef at the beginning is used to rename the structure. In our case, we have renamed the structure as Employee. so that we can address the structure in main funtion using just Employee instead of struct Employee.

  1. Write a program to read the details of ‘N’ employees (ID, Name, Salary) and display the details of those employees whose salary is above Rs.10,000/-.
    Can any one write this program.?

    1. The logic of your problem can be easily solved using the above program. Just include if(employees[i].salary> 10000) indide the loop which prints the employee details.

        for(i=0; i 10000){
              printf("Name \t: ");
              printf("%s \n",employees[i].name);
       
              printf("Id \t: ");
              printf("%d \n",employees[i].id);
       
              printf("Salary \t: ");
              printf("%d \n",employees[i].salary);
       
              printf("\n");
            }
          }
      
      1. create a structure employee (eno, ename, salary) Accept details of n employees and write a menu driven program to perform the following Quetions.
        1.isplay all employees having salary > 5000
        2.Display all employees

        Can you solve this?

  2. 1. Write a Menu Driven C program to create an employee database using structure and perform following operations: (I) Accept Employee Record (II) Display All Employees Records (III) Search Employee Record by name and Display (IV) Search Employee Record by Employee ID and Display (V) Update Employee Record (Note: Employee record will have following fields Employee ID, Employee Name, Employee Department, Employee Salary)

    can i pls get a code for this

    1. Add another char array (string) to store the date of joining.

      typedef struct{
       
          char name[30];
          int id;
          int salary;
          char dateOfJoining[20];
       
      } Employee;
    1. If you want to print an employee by his id use any of the search algorithms such as linear search or binary search to search an employee in the employee array. If found then output his details.

      int id;
      scanf("%d", &id);  
      for(int i=0; i<n; i++){
          
          if(employees[i].id == id){
              printf("Name \t: ");
              printf("%s \n",employees[i].name);
       
              printf("Id \t: ");
              printf("%d \n",employees[i].id);
       
              printf("Salary \t: ");
              printf("%d \n",employees[i].salary);
          }
          
      }
      
  3. Hi Adarsh, could you please tell me how can I compute total salary outgo for a month . For an employee using structures in C.
    Thanks in advance .

  4. Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month.

    Could you please provide a solution for this pleasee?

    1. From my side, the code is working fine. Maybe you are trying to input the full name of the employee. Can you re-check, I have modified the code.

  5. Write a C program – using structures for reading the employee details like employee name, date of joining and salary and also to compute Total salary outgo for a month ??

    can you please share code for the same .

  6. Can do you can prepare the code on this
    Create a structure to store employee’s data with the following information: Employee’s no., Employee’s name, Employee’s pay, date of joining (which is itself a structure) (i) It is decided to increase the pay as per the following rules: Pay<=Rs. underline 200 : 15 % increase Pay R . 2000: 10% increase Pay > Rs * .5000 : no increase Write a program to do this. (Assume there are 200 employees.) (ii) Write a program to print details of employees who have completed 20 years service.”

  7. Write a program to input emp no, name and salary of 100 employees using structure. Display the name and salary of employee using structure.

  8. Hey adarsh can u help me with this
    Prepare a menu driven program for employee record system
    1.Add record
    2.List record

  9. Hello, please help on how to create an employee structure to store five employee records in a linked list. The employee structure is given as:

    struct employee {
    int id;
    char name[25];
    float salary;
    } records [5];
    Thanks..

Leave a Reply to Ansh patel Cancel reply

Your email address will not be published. Required fields are marked *