Sem-2 Practical Practice Question1
Q.1 Write a program to create structure of employee with members
Empid, EmpName, Qualification and EmpSalary by taking input of 5 employees display the employee whose qualification is “MBA” and salary greater than 20000.
Solution.
#include stdio.h>
#include string.h>
#define SIZE 5
// Define structure
struct employee {
int empid;
char empname[50];
char qualification[20];
float empsalary;
};
int main() {
struct employee emp[SIZE];
// Input for 5 employees
for (int i = 0; i < SIZE; i++) {
printf("\nEnter details for Employee %d\n", i + 1);
printf("Employee ID: ");
scanf("%d", &emp[i].empid);
getchar(); // Clear newline
printf("Employee Name: ");
fgets(emp[i].empname, sizeof(emp[i].empname), stdin);
if (emp[i].empname[strlen(emp[i].empname) - 1] == '\n')
emp[i].empname[strlen(emp[i].empname) - 1] = '\0';
printf("Qualification: ");
fgets(emp[i].qualification, sizeof(emp[i].qualification), stdin);
if (emp[i].qualification[strlen(emp[i].qualification) - 1] == '\n')
emp[i].qualification[strlen(emp[i].qualification) - 1] = '\0';
printf("Salary: ");
scanf("%f", &emp[i].empsalary);
}
// Display employees with "MBA" qualification and salary > 20000
printf("\nEmployees with MBA qualification and salary > 20000:\n");
for (i = 0; i < SIZE; i++) {
if (strcmp(emp[i].qualification, "MBA") == 0 && emp[i].empsalary > 20000) {
printf("\nEmployee ID : %d", emp[i].empid);
printf("\nEmployee Name : %s", emp[i].empname);
printf("\nQualification : %s", emp[i].qualification);
printf("\nSalary : %.2f\n", emp[i].empsalary);
}
}
return 0;
}
