luc083.c ​
Metadata ​
- Author — Amit Dutta
- Last updated — 08 Feb 2026
- License — MIT License (See the LICENSE file for details)
Problem Statement ​
Problem Statement
Library menu-driven program (Add, Display, List by Author, List by Title, Count, List sorted).
Source Code ​
Printing the code
To print this file, open it on GitHub and click Raw before printing, or use the Download Raw button above and print directly from that page.
c
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct library
{
int acc_no;
char title[50];
char author[50];
float price;
int is_issued; // 1 = Yes, 0 = No
};
void add_book(struct library *lib, int *count);
void display_books(struct library *lib, int count);
void list_by_author(struct library *lib, int count);
void list_title_by_acc(struct library *lib, int count);
void sort_by_acc(struct library *lib, int count);
int main()
{
struct library books[100];
int count = 0;
int choice;
while (1)
{
printf("\n--- Library Menu ---\n");
printf("1. Add Book Info\n");
printf("2. Display Book Info\n");
printf("3. List books of given author\n");
printf("4. List title of specified accession number\n");
printf("5. List count of books\n");
printf("6. List books in order of accession number\n");
printf("7. Exit\n");
printf("Enter choice: ");
scanf("%d", &choice);
switch (choice)
{
case 1: add_book(books, &count); break;
case 2: display_books(books, count); break;
case 3: list_by_author(books, count); break;
case 4: list_title_by_acc(books, count); break;
case 5: printf("Total books in library: %d\n", count); break;
case 6: sort_by_acc(books, count); display_books(books, count); break;
case 7: exit(0);
default: printf("Invalid choice!\n");
}
}
return 0;
}
void add_book(struct library *lib, int *count)
{
printf("Enter Accession No, Title, Author, Price, Issued(1/0):\n");
scanf("%d", &lib[*count].acc_no);
scanf("%s", lib[*count].title); // Using %s for simplicity (no spaces)
scanf("%s", lib[*count].author);
scanf("%f", &lib[*count].price);
scanf("%d", &lib[*count].is_issued);
(*count)++;
}
void display_books(struct library *lib, int count)
{
int i;
for (i = 0; i < count; i++)
printf("%d: %s by %s ($%.2f) Issued: %d\n",
lib[i].acc_no, lib[i].title, lib[i].author, lib[i].price, lib[i].is_issued);
}
void list_by_author(struct library *lib, int count)
{
char author[50];
int i, found = 0;
printf("Enter author name: ");
scanf("%s", author);
for (i = 0; i < count; i++)
{
if (strcmp(lib[i].author, author) == 0)
{
printf("%s\n", lib[i].title);
found = 1;
}
}
if (!found) printf("No books found.\n");
}
void list_title_by_acc(struct library *lib, int count)
{
int acc, i;
printf("Enter Accession No: ");
scanf("%d", &acc);
for (i = 0; i < count; i++)
{
if (lib[i].acc_no == acc)
{
printf("Title: %s\n", lib[i].title);
return;
}
}
printf("Book not found.\n");
}
void sort_by_acc(struct library *lib, int count)
{
struct library temp;
int i, j;
for (i = 0; i < count - 1; i++)
{
for (j = 0; j < count - i - 1; j++)
{
if (lib[j].acc_no > lib[j + 1].acc_no)
{
temp = lib[j];
lib[j] = lib[j + 1];
lib[j + 1] = temp;
}
}
}
}1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122