
Using structure to read and print data of n employees in C
Algorithm
Input: Input the data of n employees using struct
output: Displays the data of n employees
- Start
- Input the number of employee data in a variable say n
- i = 0
- while ( i < n)
4.1 Print "Entering the data of employee", i+1
4.2 Read the name of an employee and store it in e[i].name
4.3 Read the id of an employee and store it in e[i].id
4.4 Read the salary of an employee and store it in e[i].salary
4.5 i++ - i = 0
- while ( i < n)
6.1 Print "Employee No" i+1
6.2 Print "Name: ", e[i].name
6.3 Print "Code: ", e[i].id
6.4 Print "Salary: ", e[i].salary
6.5 i++ - Stop
C Program
#include<stdio.h>
#include<string.h>
struct employee {
char name[15];
int id;
float salary;
};
int main()
{
int n;
printf("Enter the number of employee to input data: ");
scanf("%d",&n);
struct employee e[n]; // Creates an array to hold n number of employee data.
//storing inside structure
for(int i=0; i<n; i++) {
printf("Entering the data of employee no: %d\n",i+1);
printf("Enter the name of the employee: ");
scanf("%s", e[i].name);
printf("Enter the id of the empolyee: ");
scanf("%d", &e[i].id);
printf("Enter the salary of the employee: ");
scanf("%f", &e[i].salary);
}
//Printing employee data
for (int i=0; i<n; i++) {
printf("\nEmployee No %d\n",i+1);
printf("Name: %s\n", e[i].name);
printf("code: %d\n", e[i].id);
printf("Salary: %f\n", e[i].salary);
}
return 0;
}
Output
Enter the number of employee to input data: 2
Entering the data of employee no: 1
Enter the name of the employee: John
Enter the id of the empolyee: 123
Enter the salary of the employee: 5499
Entering the data of employee no: 2
Enter the name of the employee: Sherlock
Enter the id of the empolyee: 235
Enter the salary of the employee: 9899
Employee No 1
Name: John
code: 123
Salary: 5499.000000
Employee No 2
Name: Sherlock
code: 221
Salary: 9899.000000
Comments