The Program in C to store information of 10 employees and to display information of an employee depending upon the employee number input from the user is given below:
#include <stdio.h>
struct Employee {
int emp_num;
char emp_name[100];
char emp_address[100];
int emp_age;
};
void displayEmployee(struct Employee emp) {
printf("Employee Number: %d\n", emp.emp_num);
printf("Employee Name: %s\n", emp.emp_name);
printf("Employee Address: %s\n", emp.emp_address);
printf("Employee Age: %d\n", emp.emp_age);
}
int main() {
struct Employee employees[10];
int i;
int emp_num;
int found = 0;
// Input employee information
for (i = 0; i < 10; i++) {
printf("Enter information for employee %d\n", i + 1);
printf("Employee Number: ");
scanf("%d", &employees[i].emp_num);
printf("Employee Name: ");
scanf("%s", employees[i].emp_name);
printf("Employee Address: ");
scanf("%s", employees[i].emp_address);
printf("Employee Age: ");
scanf("%d", &employees[i].emp_age);
}
// Input employee number to display information
printf("Enter the employee number: ");
scanf("%d", &emp_num);
// Search for employee
for (i = 0; i < 10; i++) {
if (employees[i].emp_num == emp_num) {
found = 1;
displayEmployee(employees[i]);
break;
}
}
if (!found) {
printf("Employee not found\n");
}
return 0;
}
Output:
Enter information for employee 1
Employee Number: 1
Employee Name: John Doe
Employee Address: 123 Main St
Employee Age: 30
Enter information for employee 2
Employee Number: 2
Employee Name: Jane Smith
Employee Address: 456 Park Ave
Employee Age: 25
Enter information for employee 3
Employee Number: 3
Employee Name: Michael Johnson
Employee Address: 789 Elm St
Employee Age: 35
Enter information for employee 4
Employee Number: 4
Employee Name: David Brown
Employee Address: 321 Oak St
Employee Age: 40
Enter information for employee 5
Employee Number: 5
Employee Name: Richard Kim
Employee Address: 159 Pine St
Employee Age: 45
Enter information for employee 6
Employee Number: 6
Employee Name: Susan Lee
Employee Address: 253 Cedar St
Employee Age: 50
Enter information for employee 7
Employee Number: 7
Employee Name: Michael Davis
Employee Address: 987 Birch St
Employee Age: 55
Enter information for employee 8
Employee Number: 8
Employee Name: James Garcia
Employee Address: 654 Maple St
Employee Age: 60
Enter information for employee 9
Employee Number: 9
Employee Name: Karen Martinez
Employee Address: 319 Willow St
Employee Age: 65
Enter information for employee 10
Employee Number: 10
Employee Name: Maria Robinson
Employee Address: 753 Oak St
Employee Age: 70
Enter the employee number: 6
Employee Number: 6
Employee Name: Susan Lee
Employee Address: 253 Cedar St
Employee Age: 50
Pro-Tips💡
This program defines a struct “Employee” with four members: emp_num, emp_name, emp_address, and emp_age.
It uses an array of structs, “employees”, to store information of 10 employees.
The main function prompts the user to input the information of 10 employees and stores them in the “employees” array.
It then prompts the user to input an employee number and uses a for loop to search for the employee in the “employees” array.
If the employee is found, it calls the function “displayEmployee()” to display the information of the employee, otherwise it prints “Employee not found”.
Please note that the program uses scanf(“%d”,…) and scanf(“%s”,…) to read the input, and it does not check for the validity of the input, you may want to add some validation on the input to make sure that the input is valid.
Learn C-Sharp ↗
C-sharp covers every topic to learn about C-Sharp thoroughly.
Learn C Programming ↗
C-Programming covers every topic to learn about C-Sharp thoroughly.
Learn C++ Programming↗
C++ covers every topic to learn about C-Sharp thoroughly.