C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures and Union in C

Store hospital patient details

C Program: Store and Display Hospital Patient Details

C

#include <stdio.h>

#include <string.h>

 

// Define structure for Patient

struct Patient {

    char name[50];

    int age;

    char disease[100];

    int room_no;

};

 

int main() {

    struct Patient p[100];

    int n, i;

 

    printf("Enter number of patients: ");

    scanf("%d", &n);

    getchar(); // clear input buffer

 

    // Input patient details

    for (i = 0; i < n; i++) {

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

 

        printf("Name: ");

        gets(p[i].name);

 

        printf("Age: ");

        scanf("%d", &p[i].age);

        getchar();

 

        printf("Disease: ");

        gets(p[i].disease);

 

        printf("Room Number: ");

        scanf("%d", &p[i].room_no);

        getchar();

    }

 

    // Display patient details

    printf("\n===== PATIENT DETAILS =====\n");

    for (i = 0; i < n; i++) {

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

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

        printf("Age        : %d\n", p[i].age);

        printf("Disease    : %s\n", p[i].disease);

        printf("Room Number: %d\n", p[i].room_no);

    }

 

    return 0;

}

Output

 
OUTPUT :
Enter number of patients: 2

Enter details for Patient 1:
Name: Ramesh Kumar
Age: 45
Disease: Fever
Room Number: 102

Enter details for Patient 2:
Name: Anita Sharma
Age: 32
Disease: Asthma
Room Number: 210

===== PATIENT DETAILS =====

Patient 1 Details:
Name       : Ramesh Kumar
Age        : 45
Disease    : Fever
Room Number: 102

Patient 2 Details:
Name       : Anita Sharma
Age        : 32
Disease    : Asthma
Room Number: 210

Explanation

Concept

Description

struct Patient

Defines structure to hold patient details.

p[100]

Array of patients to store multiple records.

gets()

Reads string inputs for names and diseases. (You can use fgets() for safety.)

for loop

Handles input and output for each patient.