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:
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.
What is the difference between array and structure array
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 ofEmployee array[10]
, we mean to store structure objects into the array.Why you have write the Employee; after defining structure
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 asEmployee
. so that we can address the structure in main funtion using justEmployee
instead ofstruct Employee
.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.?
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.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?
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
How to add date of joining of the employee in the program
Add another char array (string) to store the date of joining.
In case of storing 20 employees,we need to change ?
change the value of n
if I want to print details of one employee by his id what should I do?
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.
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 .
Updated the post with the solution.
Thank you so much for the reply Adarsh . This has helped me .
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?
The above program is giving error as ” array subscipt is not an integer” kindly suggest.
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.
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 .
How can compute the total amont if the employees
how can I delete a certain employee from the list of array?
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.”
Write a program to input emp no, name and salary of 100 employees using structure. Display the name and salary of employee using structure.
Plz help me write a code to find the employee whose getting highest salary…
how we can input the n? means n should entered by users in output
Hey adarsh can u help me with this
Prepare a menu driven program for employee record system
1.Add record
2.List record
how can you delete a record?
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..