1. Home
  2. Docs
  3. C Projects
  4. Library Management System

Library Management System

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// Define structures for Book and Patron
struct Book {
    int bookId;
    char title[100];
    char author[100];
};

struct Patron {
    int patronId;
    char name[100];
};

// Function to add a book to the library
void addBook(struct Book books[], int* bookCount) {
    printf("\nEnter Book Details:\n");

    struct Book newBook;
    newBook.bookId = *bookCount + 1;

    printf("Title: ");
    scanf("%s", newBook.title);

    printf("Author: ");
    scanf("%s", newBook.author);

    books[*bookCount] = newBook;
    (*bookCount)++;

    printf("\nBook Added Successfully!\n");
}

// Function to display all books in the library
void displayBooks(struct Book books[], int bookCount) {
    printf("\nLibrary Books:\n");
    for (int i = 0; i < bookCount; i++) {
        printf("Book ID: %d, Title: %s, Author: %s\n", books[i].bookId, books[i].title, books[i].author);
    }
}

// Function to add a patron to the library
void addPatron(struct Patron patrons[], int* patronCount) {
    printf("\nEnter Patron Details:\n");

    struct Patron newPatron;
    newPatron.patronId = *patronCount + 1;

    printf("Name: ");
    scanf("%s", newPatron.name);

    patrons[*patronCount] = newPatron;
    (*patronCount)++;

    printf("\nPatron Added Successfully!\n");
}

// Function to display all patrons in the library
void displayPatrons(struct Patron patrons[], int patronCount) {
    printf("\nLibrary Patrons:\n");
    for (int i = 0; i < patronCount; i++) {
        printf("Patron ID: %d, Name: %s\n", patrons[i].patronId, patrons[i].name);
    }
}

int main() {
    struct Book libraryBooks[100];
    struct Patron libraryPatrons[100];

    int bookCount = 0;
    int patronCount = 0;

    int choice;

    do {
        printf("\nLibrary Management System\n");
        printf("1. Add Book\n");
        printf("2. Display Books\n");
        printf("3. Add Patron\n");
        printf("4. Display Patrons\n");
        printf("0. Exit\n");
        printf("Enter your choice: ");
        scanf("%d", &choice);

        switch (choice) {
            case 1:
                addBook(libraryBooks, &bookCount);
                break;
            case 2:
                displayBooks(libraryBooks, bookCount);
                break;
            case 3:
                addPatron(libraryPatrons, &patronCount);
                break;
            case 4:
                displayPatrons(libraryPatrons, patronCount);
                break;
            case 0:
                printf("\nExiting...\n");
                break;
            default:
                printf("\nInvalid Choice. Please try again.\n");
        }

    } while (choice ! = 0);

    return 0;
}

Articles

How can we help?

Leave a Reply

Your email address will not be published. Required fields are marked *