w3resource

C Program Structure: Employee Management with Highest Salary

C programming related to structures: Exercise-5 with Solution

Create a structure named "Employee" to store employee details such as employee ID, name, and salary. Write a program to input data for three employees, find the highest salary employee, and display their information.

Sample Solution:

C Code:

#include <stdio.h>

// Define the structure "Employee"
struct Employee {
    int employeeID;
    char name[50];
    float salary;
};

int main() {
    // Declare variables to store details for three employees
    struct Employee employee1, employee2, employee3;

    // Input details for the first employee
    printf("Input details for Employee 1:\n");
    printf("Employee ID: ");
    scanf("%d", &employee1.employeeID);
    printf("Name: ");
    scanf("%s", employee1.name); // Assuming names do not contain spaces
    printf("Salary: ");
    scanf("%f", &employee1.salary);

    // Input details for the second employee
    printf("\nInput details for Employee 2:\n");
    printf("Employee ID: ");
    scanf("%d", &employee2.employeeID);
    printf("Name: ");
    scanf("%s", employee2.name);
    printf("Salary: ");
    scanf("%f", &employee2.salary);

    // Input details for the third employee
    printf("\nInput details for Employee 3:\n");
    printf("Employee ID: ");
    scanf("%d", &employee3.employeeID);
    printf("Name: ");
    scanf("%s", employee3.name);
    printf("Salary: ");
    scanf("%f", &employee3.salary);

    // Find the employee with the highest salary
    struct Employee highestSalaryEmployee;
    if (employee1.salary >= employee2.salary && employee1.salary >= employee3.salary) {
        highestSalaryEmployee = employee1;
    } else if (employee2.salary >= employee1.salary && employee2.salary >= employee3.salary) {
        highestSalaryEmployee = employee2;
    } else {
        highestSalaryEmployee = employee3;
    }

    // Display information for the employee with the highest salary
    printf("\nEmployee with the Highest Salary:\n");
    printf("Employee ID: %d\n", highestSalaryEmployee.employeeID);
    printf("Name: %s\n", highestSalaryEmployee.name);
    printf("Salary: %.2f\n", highestSalaryEmployee.salary);

    return 0;
}

Output:

Input details for Employee 1:
Employee ID: 123
Name: Fabiano
Salary: 2000

Input details for Employee 2:
Employee ID: 124
Name: Larisa
Salary: 2050

Input details for Employee 3:
Employee ID: 125
Name: Mneme
Salary: 1990

Employee with the Highest Salary:
Employee ID: 124
Name: Larisa
Salary: 2050.00

Explanation:

In the exercise above,

  • The "Employee" structure is defined by members 'employeeID', 'name', and 'salary'.
  • Three variables ('employee1', 'employee2', and 'employee3') are declared to store details for three employees.
  • The program prompts the user to input details for each employee.
  • It then finds the employee with the highest salary.
  • Finally, it displays information for the highest-paid employee.

Flowchart:

Flowchart: C Program Structure: Employee Management with Highest Salary.

C Programming Code Editor:

Previous: C Program Structure: Circle area and perimeter calculation.
Next: Date Structure in C: Calculate difference between two dates.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.