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.