C program to display employee details.

Here is a C program to input employee details from the user and display the said employee based on the employee number.

#include <stdio.h>

// Define a structure for employee details
struct Employee {
    char name[100];
    int salary;
    char address[100];
};

int main() {
    struct Employee emp[50]; // Array of employees
    int n;

    printf("Enter the number of employees: ");
    scanf("%d", &n);

    for (int i = 0; i < n; i++) {
        printf("Enter employee details for employee %d:\n", i + 1);
        printf("Enter name: ");
        scanf(" %[^\n]", emp[i].name); // Read the name with spaces
        printf("Enter salary: ");
        scanf("%d", &emp[i].salary);
        printf("Enter address: ");
        scanf(" %[^\n]", emp[i].address); // Read the address with spaces
        // Print employee details
    }

    int num;
    printf("Enter the employee number to display details: ");
    scanf("%d", &num);
    printf("Type -1 to end the loop");
    while(num!=-1){
    if (num >= 0 && num < n) {
        printf("\nEmployee Details:\n");
        printf("Name: %s\n", emp[num].name);
        float tax = 0.20 * emp[num].salary;
        float netSalary = emp[num].salary - tax;
        printf("Salary: %d\n", emp[num].salary);
        printf("Employee salary with 20 percent tax: %.2f\n", netSalary);
        printf("Address: %s\n", emp[num].address);
    } else {
        printf("Invalid employee number.\n");
    }
    }
    return 0;
}

Hope you learnt something because of this blog. Dont forget to follow.

Thankyou...

0
Subscribe to my newsletter

Read articles from Gagan G Saralaya directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Gagan G Saralaya
Gagan G Saralaya