0% found this document useful (0 votes)
29 views

Exp 2

Uploaded by

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

Exp 2

Uploaded by

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

Case Study: Library Management System

Problem Statement

Design and implement a Library Management System that allows the following operations:

1. Add a new book to the library.


2. Display all books in the library.
3. Borrow a book from the library.

Design

We'll use a structure to represent a book and an array to store multiple books. The system will
have a menu-driven interface to perform the operations.

#include <stdio.h>

#include <string.h>

// Define the maximum number of books

#define MAX_BOOKS 100

// Define a structure for a book

typedef struct {

int id;

char title[50];

char author[50];

int available; // 1 if available, 0 if borrowed

} Book;

// Declare an array to store books

Book library[MAX_BOOKS];

int book_count = 0;

// Function prototypes

void addBook();
void displayBooks();

void borrowBook();

int main() {

int choice;

while (1) {

printf("\nLibrary Management System\n");

printf("1. Add a new book\n");

printf("2. Display all books\n");

printf("3. Borrow a book\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch (choice) {

case 1:

addBook();

break;

case 2:

displayBooks();

break;

case 3:

borrowBook();

break;

case 4:

return 0;

default:

printf("Invalid choice. Please try again.\n");


}

return 0;

void addBook() {

if (book_count >= MAX_BOOKS) {

printf("Library is full. Cannot add more books.\n");

return;

Book new_book;

new_book.id = book_count + 1;

printf("Enter the title of the book: ");

getchar(); // Consume the newline character left by previous input

fgets(new_book.title, sizeof(new_book.title), stdin);

new_book.title[strcspn(new_book.title, "\n")] = 0; // Remove the trailing newline character

printf("Enter the author of the book: ");

fgets(new_book.author, sizeof(new_book.author), stdin);

new_book.author[strcspn(new_book.author, "\n")] = 0; // Remove the trailing newline character

new_book.available = 1;

library[book_count] = new_book;

book_count++;

printf("Book added successfully.\n");

}
void displayBooks() {

if (book_count == 0) {

printf("No books in the library.\n");

return;

printf("\nBooks in the library:\n");

for (int i = 0; i < book_count; i++) {

printf("ID: %d\n", library[i].id);

printf("Title: %s\n", library[i].title);

printf("Author: %s\n", library[i].author);

printf("Available: %s\n", library[i].available ? "Yes" : "No");

printf("----------------------\n");

void borrowBook() {

int book_id;

printf("Enter the ID of the book to borrow: ");

scanf("%d", &book_id);

if (book_id < 1 || book_id > book_count) {

printf("Invalid book ID.\n");

return;

if (library[book_id - 1].available == 0) {

printf("Book is already borrowed.\n");

} else {
library[book_id - 1].available = 0;

printf("Book borrowed successfully.\n");

You might also like