Pages

Monday, February 12, 2024

Define A Structure Employee Having Elements Emp_id, Name, address , DOB. Accept Data And Reprint It. (Use Structure Within Structure) & display only whose address is in “pokhara”

 

Nested Structure

 



 

#include <stdio.h>

#include <string.h>

 

// Define a structure for date

struct Date {

    int day;

    int month;

    int year;

};

 

// Define a structure for an employee

struct Employee {

    int emp_id;

    char name[50];

    char address[100];

    struct Date dob; // Date of Birth

};

 

int main() {

    // Declare an array of employees

    struct Employee employees[5];

 

    // Accept data for each employee

    for (int i = 0; i < 5; ++i) {

        printf("Enter details for Employee %d:\n", i + 1);

        printf("Employee ID: ");

        scanf("%d", &employees[i].emp_id);

        printf("Name: ");

        scanf("%s", employees[i].name);

        printf("Address: ");

        scanf("%s", employees[i].address);

        printf("Date of Birth (DD MM YYYY): ");

        scanf("%d %d %d", &employees[i].dob.day, &employees[i].dob.month, &employees[i].dob.year);

    }

 

    // Reprint the data for employees with address in Pokhara

    printf("\nEmployee Data in Pokhara:\n");

    for (int i = 0; i < 5; ++i) {

        if (strcmp(employees[i].address, "Pokhara") == 0) {

            printf("Employee %d:\n", i + 1);

            printf("Employee ID: %d\n", employees[i].emp_id);

            printf("Name: %s\n", employees[i].name);

            printf("Address: %s\n", employees[i].address);

            printf("Date of Birth: %02d-%02d-%04d\n", employees[i].dob.day, employees[i].dob.month, employees[i].dob.year);

            printf("\n");

        }

    }

 

    return 0;

}