The Wayback Machine - https://web.archive.org/web/20240609184409/https://www.geeksforgeeks.org/c-program-for-hospital-management-system/
Open In App

Hospital Management System in C

Last Updated : 25 Sep, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

A Hospital Management System is software that facilitates its users some basic operations that take place in a typical hospital. This Hospital Management System in C language is a project for Hospitals having the following functionalities:

  • Printing Hospital Data
  • Print Patients Data
  • Sort by Bed Price
  • Sort by Available Beds
  • Sort by Name
  • Sort by Rating and Reviews
  • Print the Hospital’s Name in a Specific City

All the functions will be operated through switch cases in the main function. There will be a menu for users for easy navigation.

Components of the Project

1. Structure for Hospital and Patients Data

The Structure for Hospital’s and patient’s data is defined below:

C




// Structure for Hospital
struct Hospital {
    char name[50];
    char city[50];
    int beds;
    float price;
    float rating;
    int reviews;
};
  
//Structure for Patient
struct Patient {
    char name[50];
    int age;
};


2. Function to Print Hospital Data

This function prints all the hospital data.

C




// Function to print hospital data
void printHospital(struct Hospital hosp) {
    printf("Hospital Name: %s\n", hosp.name);
    printf("City: %s\n", hosp.city);
    printf("Total Beds: %d\n", hosp.beds);
    printf("Price per Bed: $%.2f\n", hosp.price);
    printf("Rating: %.1f\n", hosp.rating);
    printf("Reviews: %d\n", hosp.reviews);
    printf("\n");
}


3. Function to Print all Patients Data

This Function prints all patient data.

C




// Function to print patient data
void printPatient(struct Patient patient) {
    printf("Patient Name: %s\n", patient.name);
    printf("Age: %d\n", patient.age);
    printf("\n");
}


4. Function to Sort Hospitals by Bed’s Price

This function sorts the bed prices of each hospital in ascending order.

C




// Function to sort hospitals by beds price (ascending)
void sortByPrice(struct Hospital hospitals[], int n) {
    // Implement sorting logic: bubble sort
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].price > hospitals[j + 1].price) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}


5. Function to Sort Hospitals by Available Beds

This function sorts hospitals by number of available beds in descending order.

C




// Function to sort hospitals by available beds (descending)
void sortByBeds(struct Hospital hospitals[], int n) {
    // Implement sorting logic: bubble sort
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].beds < hospitals[j + 1].beds) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}


6. Function to Sort Hospitals by Name

This function sorts hospitals by name in alphabetical order.

C




// Function to sort hospitals by name (ascending)
void sortByName(struct Hospital hospitals[], int n) {
    // Using strcmp
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (strcmp(hospitals[j].name, hospitals[j + 1].name) > 0) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}


7. Function to Sort Hospitals by Rating and Reviews

This function sorts hospitals by number of reviews.

The logic used here is: If the hospital A’s rating*reviews < Hospital B’s rating*reviews then swap them.

C




// Function to sort hospitals by rating and reviews (descending)
void sortByRating(struct Hospital hospitals[], int n) {
    // Based on Reviews
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].rating * hospitals[j].reviews < hospitals[j + 1].rating * hospitals[j + 1].reviews) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}


8. Function Print the Hospitals of any Specific City

This function takes input the name of city and returns the hospitals present in the specific city.

C




// Function to print hospitals in a specific city
void printHospitalsInCity(struct Hospital hospitals[]) {
    char city[50];
    int hospitalsFound = 0;  
  
    printf("Enter city name (X, Y or Z): ");
    scanf("%s", city);
  
    printf("Hospitals in %s:\n", city);
  
    for (int i = 0; i < 5; i++) {
        // Use strcasecmp for case-insensitive comparison
        if (strcasecmp(hospitals[i].city, city) == 0) {
            printf("Hospital Name: %s\n", hospitals[i].name);
            printf("City: %s\n", hospitals[i].city);
            printf("Total Beds: %d\n", hospitals[i].beds);
            printf("Price per Bed: $%.2f\n", hospitals[i].price);
            printf("Rating: %.1f\n", hospitals[i].rating);
            printf("Reviews: %d\n", hospitals[i].reviews);
            printf("\n");
            hospitalsFound++;
        }
    }
  
    if (hospitalsFound == 0) {
        printf("No hospitals found in %s\n", city);
    }
}


9. Main Function

This is the main function to operate the Hospital management system in c.

C




int main() {
    // Sample hospital data
    struct Hospital hospitals[5] = {
        {"Hospital A", "X", 100, 250.0, 4.5, 100},
        {"Hospital B", "Y", 150, 200.0, 4.2, 80},
        {"Hospital C", "X", 200, 180.0, 4.0, 120},
        {"Hospital D", "Z", 80, 300.0, 4.8, 90},
        {"Hospital E", "Y", 120, 220.0, 4.6, 110}
    };
  
    // Sample patient data (associated with hospitals)
    struct Patient patients[5][3] = {
        {{"Amar", 35}, {"Manish", 45}, {"Atul", 28}},
        {{"Elvish", 62}, {"Debolina", 18}, {"Shruti", 55}},
        {{"Zafar", 50}, {"Rahul", 30}, {"Priya", 40}},
        {{"Amir", 22}, {"Asif", 38}, {"Prince", 60}},
        {{"Aditya", 28}, {"Aman", 48}, {"Sahil", 33}}
    };
  
    int n = 5;  // Number of hospitals
  
    int choice;
    char city[50];
  
    do {
        printf("\n\n\n*********** Hospital Management System Menu:************\n\n");
        printf("1. Printing Hospital Data\n");
        printf("2. Printing Patients Data\n");
        printf("3. Sorting Hospitals by Beds Price\n");
        printf("4. Sorting Hospitals by Available Beds\n");
        printf("5. Sorting Hospitals by Name\n");
        printf("6. Sorting Hospitals by Rating and Reviews\n");
        printf("7. Print Hospitals in a Specific City\n");
        printf("8. Exit\n\n");
        printf("Enter your choice: ");
        scanf("%d", &choice;);
  
        switch (choice) {
            case 1:
                printf("\nPrinting Hospital Data:\n\n");
                for (int i = 0; i < n; i++) {
                    printHospital(hospitals[i]);
                }
                break;
            case 2:
                printf("Printing Patients Data:\n\n");
                for (int i = 0; i < n; i++) {
                    printf("Hospital: %s\n", hospitals[i].name);
                    for (int j = 0; j < 3; j++) {
                        printPatient(patients[i][j]);
                    }
                }
                break;
            case 3:
                printf("Sorting Hospitals by Beds Price (Ascending):\n");
                sortByBeds(hospitals, n);
                for (int i = 0; i < n; i++) {
                    printHospital(hospitals[i]);
                }
                break;
            case 4:
                printf("Sorting Hospitals by Available Beds (Descending):\n");
                sortByBeds(hospitals, n);  // Fix: Sorting by available beds
                for (int i = 0; i < n; i++) {
                    printHospital(hospitals[i]);
                }
                break;
            case 5:
                printf("Sorting Hospitals by Name (Ascending):\n");
                sortByName(hospitals, n);
                for (int i = 0; i < n; i++) {
                    printHospital(hospitals[i]);
                }
                break;
            case 6:
                printf("Sorting Hospitals by Rating and Reviews (Descending):\n");
                sortByRating(hospitals, n);
                for (int i = 0; i < n; i++) {
                    printHospital(hospitals[i]);
                }
                break;
            case 7:
                printHospitalsInCity(hospitals);
                break;
            case 8:
                printf("Exiting the program.\n");
                break;
            default:
                printf("Invalid choice. Please enter a valid option.\n");
        }
    } while (choice != 8);
  
    return 0;
}


C Program for Hospital Management

C




// C Program to implement Hospital Management System
#include <ctype.h> // Include ctype.h for strcasecmp
#include <stdio.h>
#include <string.h>
  
// Define a structure for Hospital
struct Hospital {
    char name[50];
    char city[50];
    int beds;
    float price;
    float rating;
    int reviews;
};
  
// Define a structure for Patient
struct Patient {
    char name[50];
    int age;
};
  
// Function to print hospital data
void printHospital(struct Hospital hosp)
{
    printf("Hospital Name: %s\n", hosp.name);
    printf("City: %s\n", hosp.city);
    printf("Total Beds: %d\n", hosp.beds);
    printf("Price per Bed: $%.2f\n", hosp.price);
    printf("Rating: %.1f\n", hosp.rating);
    printf("Reviews: %d\n", hosp.reviews);
    printf("\n");
}
  
// Function to sort hospitals by beds price (ascending)
void sortByPrice(struct Hospital hospitals[], int n)
{
    // Implement sorting logic (e.g., bubble sort)
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].price
                > hospitals[j + 1].price) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}
  
// Function to sort hospitals by name (ascending)
void sortByName(struct Hospital hospitals[], int n)
{
    // Implement sorting logic (e.g., using strcmp)
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (strcmp(hospitals[j].name,
                       hospitals[j + 1].name)
                > 0) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}
  
// Function to sort hospitals by rating and reviews
// (descending)
void sortByRating(struct Hospital hospitals[], int n)
{
    // Implement sorting logic (e.g., based on rating and
    // reviews)
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].rating * hospitals[j].reviews
                < hospitals[j + 1].rating
                      * hospitals[j + 1].reviews) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}
  
// Function to print hospitals in a specific city
// (case-insensitive)
void printHospitalsInCity(struct Hospital hospitals[])
{
    char city[50];
    int hospitalsFound
        = 0; // Counter for hospitals found in the city
  
    printf("Enter city name (X, Y or Z): ");
    scanf("%s", city);
  
    printf("Hospitals in %s:\n", city);
  
    for (int i = 0; i < 5; i++) {
        // Use strcasecmp for case-insensitive comparison
        if (strcasecmp(hospitals[i].city, city) == 0) {
            printf("Hospital Name: %s\n",
                   hospitals[i].name);
            printf("City: %s\n", hospitals[i].city);
            printf("Total Beds: %d\n", hospitals[i].beds);
            printf("Price per Bed: $%.2f\n",
                   hospitals[i].price);
            printf("Rating: %.1f\n", hospitals[i].rating);
            printf("Reviews: %d\n", hospitals[i].reviews);
            printf("\n");
            hospitalsFound++;
        }
    }
  
    if (hospitalsFound == 0) {
        printf("No hospitals found in %s\n", city);
    }
}
  
// Function to sort hospitals by available beds (descending)
void sortByBeds(struct Hospital hospitals[], int n)
{
    // Implement sorting logic (e.g., bubble sort)
    for (int i = 0; i < n - 1; i++) {
        for (int j = 0; j < n - i - 1; j++) {
            if (hospitals[j].beds < hospitals[j + 1].beds) {
                struct Hospital temp = hospitals[j];
                hospitals[j] = hospitals[j + 1];
                hospitals[j + 1] = temp;
            }
        }
    }
}
  
// Function to print patient data
void printPatient(struct Patient patient)
{
    printf("Patient Name: %s\n", patient.name);
    printf("Age: %d\n", patient.age);
    printf("\n");
}
  
int main()
{
    // Sample hospital data
    struct Hospital hospitals[5]
        = { { "Hospital A", "X", 100, 250.0, 4.5, 100 },
            { "Hospital B", "Y", 150, 200.0, 4.2, 80 },
            { "Hospital C", "X", 200, 180.0, 4.0, 120 },
            { "Hospital D", "Z", 80, 300.0, 4.8, 90 },
            { "Hospital E", "Y", 120, 220.0, 4.6, 110 } };
  
    // Sample patient data (associated with hospitals)
    struct Patient patients[5][3] = { { { "Amar", 35 },
                                        { "Manish", 45 },
                                        { "Atul", 28 } },
                                      { { "Elvish", 62 },
                                        { "Debolina", 18 },
                                        { "Shruti", 55 } },
                                      { { "Zafar", 50 },
                                        { "Rahul", 30 },
                                        { "Priya", 40 } },
                                      { { "Amir", 22 },
                                        { "Asif", 38 },
                                        { "Prince", 60 } },
                                      { { "Aditya", 28 },
                                        { "Aman", 48 },
                                        { "Sahil", 33 } } };
  
    int n = 5; // Number of hospitals
  
    int choice;
    char city[50];
  
    do {
        printf("\n\n\n*********** Hospital Management "
               "System Menu:************\n\n");
        printf("1. Printing Hospital Data\n");
        printf("2. Printing Patients Data\n");
        printf("3. Sorting Hospitals by Beds Price\n");
        printf("4. Sorting Hospitals by Available Beds\n");
        printf("5. Sorting Hospitals by Name\n");
        printf(
            "6. Sorting Hospitals by Rating and Reviews\n");
        printf("7. Print Hospitals in a Specific City\n");
        printf("8. Exit\n\n");
        printf("Enter your choice: ");
        scanf("%d", &choice;);
  
        switch (choice) {
        case 1:
            printf("\nPrinting Hospital Data:\n\n");
            for (int i = 0; i < n; i++) {
                printHospital(hospitals[i]);
            }
            break;
        case 2:
            printf("Printing Patients Data:\n\n");
            for (int i = 0; i < n; i++) {
                printf("Hospital: %s\n", hospitals[i].name);
                for (int j = 0; j < 3; j++) {
                    printPatient(patients[i][j]);
                }
            }
            break;
        case 3:
            printf("Sorting Hospitals by Beds Price "
                   "(Ascending):\n");
            sortByBeds(hospitals, n);
            for (int i = 0; i < n; i++) {
                printHospital(hospitals[i]);
            }
            break;
        case 4:
            printf("Sorting Hospitals by Available Beds "
                   "(Descending):\n");
            sortByBeds(hospitals,
                       n); // Fix: Sorting by available beds
            for (int i = 0; i < n; i++) {
                printHospital(hospitals[i]);
            }
            break;
        case 5:
            printf(
                "Sorting Hospitals by Name (Ascending):\n");
            sortByName(hospitals, n);
            for (int i = 0; i < n; i++) {
                printHospital(hospitals[i]);
            }
            break;
        case 6:
            printf("Sorting Hospitals by Rating and "
                   "Reviews (Descending):\n");
            sortByRating(hospitals, n);
            for (int i = 0; i < n; i++) {
                printHospital(hospitals[i]);
            }
            break;
        case 7:
            printHospitalsInCity(hospitals);
            break;
        case 8:
            printf("Exiting the program.\n");
            break;
        default:
            printf("Invalid choice. Please enter a valid "
                   "option.\n");
        }
    } while (choice != 8);
  
    return 0;
}


Output:



Similar Reads

Create a Hospital Management System using PHP and MySQL
The Hospital Management System (HMS) is a robust and efficient solution designed to streamline the processes within a healthcare facility. This project is built using PHP and MySQL, offering a user-friendly interface for managing patient information, appointments, and other essential aspects of hospital administration. PreviewApproachThe approach i
5 min read
Hospital Management Application using MERN Stack
In the fast-paced world of healthcare, it's vital to manage hospital tasks effectively to ensure top-notch patient care. This article explores creating a strong Hospital Management App using the MERN stack – that's MongoDB, Express, React, and Node.js, breaking down the process for easier understanding. Preview of final output: Let us have a look a
16 min read
Hospital Appointment System using Express
Hospital Appointment System project using Express and MongoDB contains various endpoints that will help to manage hospital appointments. In this project, there is an appointment endpoint for user management and appointment management. API will be able to register users, authenticate users, book appointments, and cancel appointments. We'll be using
12 min read
Design Restaurant Management System | System Design
In the modern restaurant industry, delivering exceptional dining experiences requires more than just good cuisine. Restaurant Management Systems have emerged as the secret ingredient for efficient operations and customer satisfaction. We are going to design a restaurant management system where users can effortlessly discover nearby restaurants. The
13 min read
Product Management Process | 7 stages of product management
Product management is a process of creating and managing products that people want to buy. It involves many different steps, from understanding customer needs to launching and maintaining a product. The product management process consists of seven different stages, each important for navigating the complex journey from ideation to market success. T
5 min read
How to Plan Expectation Management in Project Management?
Planning expectation management in project management is about setting the project goals, making a plan to execute the project, making a budget plan for resources, and executing the plan. Establishing communication with team members, stakeholders, and clients reduces misunderstandings. Update the project progress to the stakeholders and take feedba
4 min read
What are the Five Steps in Risk Management Process in project management?
Risks are an inherent part of any Project that has been initiated to deliver some end product to the customers. This is why Project Professionals always need to take care of the potential disruptions in the middle of the Project phases. This article focuses on discussing the Risk Management Process and its steps in detail. Table of Content What is
8 min read
Program for Employee Management System
A Employee’s Management System (EMS) is a software built to handle the primary housekeeping functions of a company. EMS help companies keep track of all the employees and their records. It is used to manage the company using computerized system. Aim of Employee’s Management System: Built The Employee Table.Insert New Entries.Delete The Entries.Sear
14 min read
Employee Management System using doubly linked list in C
Design and implement a menu-driven program in C for the below operations on DLL of employee data with fields: SSN, name, department, designation, Salary, Phone Number: Create a DLL of N employee's data by using end insertion.Display the status of DLL and count the number of nodes in it.Perform insertion and deletion at end of DLL.Perform insertion
6 min read
Department Store Management System(DSMS) using C++
A Department Store Management System (DSMS) in C++ is a software tool used to manage the inventory and sales of items in a department store. In this article, we will be discussing the implementation of a DSMS on a command-line user interface using C++. The system allows various features to admin such as add, view, update, and delete items in the st
8 min read