C Program to Store Information of 10 Employees Using Structure
Objective
Write a C program to store information of 10 Employees using an Array of Structures.
Algorithm / Approach
- Define
struct Employee. - In
main(), declare an array of structures:struct Employee s[10]. - Run a loop from 0 to 9.
- Inside the loop, access each employee's data using the array index and the dot operator:
s[i].nameand&s[i].salary. - Use a second loop to print out all the stored data.
main.c
#include<stdio.h>
struct Employee{
char name[50];
int id;
float salary;
};
int main( ){
struct Employee s[10];
int i;
printf("Enter Info of Emps : \n");
for(i=0;i<10;++i) {
s[i].id=i+1;
printf("\nFor id number %d\n",s[i].id);
printf("Enter name: ");
scanf("%s",s[i].name);
printf("Enter salary: ");
scanf("%f",&s[i].salary);
printf("\n");
}
printf("Information of Emps:\n");
for(i=0;i<10;++i) {
printf("Info for id number %d:\n",i+1);
printf("Name : ");
puts(s[i].name);
printf("salary : %.1f",s[i].salary);
}
return 0;
}
Expected Output
Enter Info of Emps : For id number 1 Enter Name : xyz Enter Salary : 5000 For id number 2 Enter Name : abc Enter Salary : 15000 . . Information of Emps : Info for id number 1 Name : xyz Salary : 5000 Name : abc Salary : 15000 . .
Explanation of the Program
- An Array of Structures is incredibly powerful. Instead of making 10 separate variables (s1, s2, s3...), we make an array of size 10.
- Each slot in the array (e.g.,
s[0]) holds a complete Employee structure, allowing us to easily process hundreds of records using a simple loop, just like a database table.
Complexity
Time Complexity
O(n) - Where n is the number of employees (10).
Space Complexity
O(n) - Array of 10 structures.