Write a C program of library management system using switch case

A library management system in C allows you to store and manage book information efficiently. This program uses a structure to store book details and implements a menu-driven interface using switch-case statements for different operations.

Syntax

struct library {
    char bookname[50];
    char author[50];
    int noofpages;
    float price;
};

Algorithm

Step 1: Declare a structure to hold book data members
Step 2: Declare variables for loop control and book counting
Step 3: Use switch case to handle different operations
Step 4: Case 1 - Add book information
        Case 2 - Display book information  
        Case 3 - Show total number of books
        Case 4 - Exit the program

Example: Library Management System

The following program demonstrates a complete library management system with add, display, count, and exit functionalities −

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

struct library {
    char bookname[50];
    char author[50];
    int noofpages;
    float price;
};

int main() {
    struct library lib[100];
    int i = 0, choice, keepcount = 0;
    
    while (1) {
        printf("<br>=== Library Management System ===<br>");
        printf("1. Add book information<br>");
        printf("2. Display book information<br>");
        printf("3. Number of books in library<br>");
        printf("4. Exit<br>");
        printf("\nEnter your choice: ");
        scanf("%d", &choice);
        
        switch (choice) {
            case 1:
                printf("\nEnter book name: ");
                scanf("%s", lib[keepcount].bookname);
                printf("Enter author name: ");
                scanf("%s", lib[keepcount].author);
                printf("Enter number of pages: ");
                scanf("%d", &lib[keepcount].noofpages);
                printf("Enter price: ");
                scanf("%f", &lib[keepcount].price);
                keepcount++;
                printf("Book added successfully!<br>");
                break;
                
            case 2:
                if (keepcount == 0) {
                    printf("\nNo books in the library!<br>");
                } else {
                    printf("<br>=== Book Information ===<br>");
                    for (i = 0; i < keepcount; i++) {
                        printf("\nBook %d:<br>", i + 1);
                        printf("Name: %s<br>", lib[i].bookname);
                        printf("Author: %s<br>", lib[i].author);
                        printf("Pages: %d<br>", lib[i].noofpages);
                        printf("Price: %.2f<br>", lib[i].price);
                    }
                }
                break;
                
            case 3:
                printf("\nTotal books in library: %d<br>", keepcount);
                break;
                
            case 4:
                printf("Exiting program. Thank you!<br>");
                exit(0);
                
            default:
                printf("Invalid choice! Please try again.<br>");
        }
    }
    
    return 0;
}

Output

=== Library Management System ===
1. Add book information
2. Display book information
3. Number of books in library
4. Exit

Enter your choice: 1

Enter book name: HarryPotter
Enter author name: JKRowling
Enter number of pages: 350
Enter price: 450.50
Book added successfully!

=== Library Management System ===
1. Add book information
2. Display book information
3. Number of books in library
4. Exit

Enter your choice: 2

=== Book Information ===

Book 1:
Name: HarryPotter
Author: JKRowling
Pages: 350
Price: 450.50

=== Library Management System ===
1. Add book information
2. Display book information
3. Number of books in library
4. Exit

Enter your choice: 3

Total books in library: 1

=== Library Management System ===
1. Add book information
2. Display book information
3. Number of books in library
4. Exit

Enter your choice: 4
Exiting program. Thank you!

Key Features

  • Structure Usage: Organizes book data (name, author, pages, price) efficiently
  • Menu-Driven Interface: User-friendly options for different operations
  • Dynamic Storage: Can store up to 100 books in the array
  • Input Validation: Handles invalid menu choices gracefully

Conclusion

This library management system demonstrates effective use of structures and switch-case statements in C. It provides a foundation for more advanced features like book search, deletion, and file storage operations.

Updated on: 2026-03-15T13:40:20+05:30

10K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements