Employee Example With Specific Salary


Explanation

An array of structure is used to store multiple employee records. We can apply conditions on structure members to display only specific records according to the given requirement.

In this program, employee details such as employee id, name, and salary are stored using an array of structures. The program checks the salary of each employee and displays only those employees whose salary is greater than 15000.

How It Works?

  • Step 1: Create a structure named employee with members id, name, and salary.
  • Step 2: Create an array of structure e[5] to store multiple employee records.
  • Step 3: Accept details of three employees using a for loop.
  • Step 4: Use another for loop to check each employee's salary.
  • Step 5: If the employee's salary is greater than 15000, display that employee's details.

Example Program


#include <stdio.h>
#include <conio.h>

struct employee
{

    char name[20];

    int id, salary;

};


main()
{

    struct employee e[5];

    int k;

    clrscr();


    for(k = 0; k <= 2; k++)

    {

        printf("Enter the Employee Id : ");

        scanf("%d", &e[k].id);


        printf("Enter the Employee Name : ");

        scanf("%s", e[k].name);


        printf("Enter the Employee Salary : ");

        scanf("%d", &e[k].salary);

    }


    printf("\nId \tName \tSalary");


    for(k = 0; k <= 2; k++)

    {

        if(e[k].salary > 15000)

        {

            printf("\n%d", e[k].id);

            printf("\t%s", e[k].name);

            printf("\t%d", e[k].salary);

        }

    }


    getch();

}

Output

Enter the Employee Id : 101
Enter the Employee Name : Amit
Enter the Employee Salary : 12000

Enter the Employee Id : 102
Enter the Employee Name : Rahul
Enter the Employee Salary : 25000

Enter the Employee Id : 103
Enter the Employee Name : Neha
Enter the Employee Salary : 35000

Id    Name    Salary
102   Rahul   25000
103   Neha    35000

Practice Exercises

Task 1: Write a C program using an array of structure to store details of 5 employees and display only those employees whose salary is greater than 50000.

Goal: Learn how to use conditions with structure members and display specific records from an array of structures.

💡 Hint: Create a structure named employee with members id, name, and salary. Store employee records using an array of structures. Use an if condition to check whether salary > 50000 before displaying employee details.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct employee
{

    char name[20];

    int id, salary;

};


main()
{

    struct employee e[5];

    int k;

    clrscr();


    for(k = 0; k <= 2; k++)

    {

        printf("Enter Employee Id : ");

        scanf("%d", &e[k].id);


        printf("\nEnter Employee Name : ");

        scanf("%s", e[k].name);


        printf("\nEnter Employee Salary : ");

        scanf("%d", &e[k].salary);

    }


    printf("\nId \tName \tSalary");


    for(k = 0; k <= 2; k++)

    {

        if(e[k].salary > 50000)

        {

            printf("\n%d", e[k].id);

            printf("\t%s", e[k].name);

            printf("\t%d", e[k].salary);

        }

    }


    getch();

}

Output
Enter Employee Id : 101
Enter Employee Name : Amit
Enter Employee Salary : 40000

Enter Employee Id : 102
Enter Employee Name : Rahul
Enter Employee Salary : 65000

Enter Employee Id : 103
Enter Employee Name : Neha
Enter Employee Salary : 80000

Id    Name    Salary
102   Rahul   65000
103   Neha    80000
Explanation: The program stores employee details using an array of structures. Each employee's salary is checked using an if condition. Only employees having salary greater than 50000 are displayed.