0% found this document useful (0 votes)
954 views5 pages

Library Management System Using C

The document describes a C program for a library management system. It defines structures to store book and library data, and functions to add, remove, and display books. The main function initializes an array of books, displays a menu, and calls the appropriate function based on the user's input. The program uses structures to organize book details, functions to encapsulate tasks, and a menu-driven interface to manage the library.

Uploaded by

Vedant Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
954 views5 pages

Library Management System Using C

The document describes a C program for a library management system. It defines structures to store book and library data, and functions to add, remove, and display books. The main function initializes an array of books, displays a menu, and calls the appropriate function based on the user's input. The program uses structures to organize book details, functions to encapsulate tasks, and a menu-driven interface to manage the library.

Uploaded by

Vedant Gade
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Library management system using C

 Algorithm
Define the maximum number of books, maximum length of book title, and
maximum length of author name using #define.
Define a structure named "Book" that contains four elements:
A. title - character array of length MAX_TITLE_LEN to store the title of the
book.
B. author - character array of length MAX_AUTHOR_LEN to store the
name of the author.
C. id - integer to store the unique ID of the book.
d. num_copies - integer to store the number of copies of the book. Define
another structure named "Library" that contains two elements:
A.books - an array of Book structures of length MAX_BOOKS to
store all the books in the library.
b. num_books - integer to store the current number of books in the library.
Define a function named "display_books" that takes a Library structure as an
argument and displays all the books in the library along with their details.
Define a function named "add_book" that takes a pointer to a Library
structure as an argument and adds a new book to the library. a. Check if the
library is full.
A. If not, get the title, author name, ID, and number of copies of
the new book from the user.
c. Add the new book to the books array of the library.
d. Increment the num_books variable of the library.
Define a function named "remove book" that takes a pointer to a Library
structure and an ID of the book to be removed as arguments and removes the
book from the library.
a. Find the index of the book in the books array of the library using its ID.
b. If the book is found, remove it by shifting all the elements in the array
after it one position to the left.
c. Decrement the num_books variable of the library.
In the main function, create a new Library structure with num_books
initialized to 0.
Define a loop that displays a menu of options for the user to choose from.
Get the user's choice from the menu.
Depending on the user's choice, call the appropriate function. If the
user chooses to quit, exit the program.

 Flowchart
+------------------------------------------------+
| Start Program |
+------------------------------------------------+

|
|
+------------------------------------------------+
| Initialize library and variables |
+------------------------------------------------+

|
|

+------------------------------------------------+
| Display menu and accept user input |

+ -------------------------------------------- +

|
|

+---------------------------------------------------------+
| Check user input and perform action |
+---------------------------------------------------------+

| | |
| | |

+---------------------------------------------------------+
| Display books Add book Remove book |
+---------------------------------------------------------+

| | |
| | |

+------+ +------+ +------+


| Exit | | Exit | | Exit |
+------+ +------+ +------+

C program code

#include <stdio.h>
#include <string.h>
// define maximum number of books in the library
#define MAX_BOOKS 100

// define structure for book information


struct book {
int id;
char title[50];
char author[50];
int year;
int available;
};

// Function to add a new book to the library


void add_book(struct book library[], int *num_books) {
if (*num_books == MAX_BOOKS) {
printf ("Library is full.\n");
return;
}
struct book new_book;
printf("Enter book ID: ");
scanf("%d", &new_book.id);
printf("Enter book title: ");
getchar();
fgets(new_book.title, 50, stdin);
printf("Enter book author: ");
fgets(new_book.author, 50, stdin);
printf("Enter year of publication: ");
scanf("%d", &new_book.year);
new_book.available = 1;
library[*num_books] = new_book;
*num_books += 1;
printf("Book added to the library.\n");
}

// Function to remove a book from the library


void remove_book(struct book library[], int *num_books) {
int id, i;
printf("Enter book ID to remove: ");
scanf("%d", &id);
for (i = 0; i < *num_books; i++) {
if (library[i].id == id) {
library[i] = library[*num_books - 1];
*num_books -= 1;
printf("Book removed from the library.\n");
return;
}
}
printf("Book not found.\n");
}

// Function to display all books in the library


void display_books(struct book library[], int num_books) {
int i;
printf("ID\tTitle\tAuthor\tYear\tAvailability\n");
for (i = 0; i < num_books; i++) {
printf("%d\t%s\t%s\t%d\t%s\n", library[i].id, library[i].title, library[i].author, library[i].year,
library[i].available ? "Available" : "Not available");
}
}
int main() {
struct book library[MAX_BOOKS];
int num_books = 0;
int choice;
do {
printf("\nLibrary Management System\n");
printf("1. Add a new book\n");
printf("2. Remove a book\n");
printf("3. Display all books\n");
printf("4. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
add_book(library, &num_books);
break;
case 2:
remove_book(library, &num_books);
break;
case 3:
display_books(library, num_books);
break;
case 4:
printf("Goodbye!\n");1
break;
default:
printf("Invalid choice.\n");
}
} while (choice != 4);
return 0;
}

1
This is a C program for library management system

You might also like