Introduction
In this post, you can find a c program to store and display data of 100 books. For this, we must know the use of structure. The structure is a variable or group where we store different type of data.
Here, we have to store and display data of 100 books. The data or information of a book is different. What type of information of books we are storing and displaying?
Also Read: Basic 50 Programs in C
This information can be very long. Here, we are storing the book title, author, publication and price of the books. See the following program.
C Program to Store and Display Data of 100 Books
#include <stdio.h>
#include <stdlib.h>
int main()
{
struct book
{
char title[100],author[25],publication[25];
float price;
};
struct book b[100];
int i;
for(i=0;i<100;i++)
{
printf("Enter Data of Book %d\n",i+1);
printf("Title: ");
gets(b[i].title);
printf("Author: ");
gets(b[i].author);
printf("Publication: ");
gets(b[i].publication);
printf("Price: ");
scanf("%f",&b[i].price);
}
for(i=0;i<100;i++)
{
printf("\t\t\t\tBook %d\n",i+1);
printf("Title: %s",b[i].title);
printf("\nAuthor: %s",b[i].author);
printf("\nPublication: %s",b[i].publication);
printf("\nPrice: %0.2f",b[i].price);
}
return 0;
}
As you can see the above program, I have used structure. It helps us to group the different type of data. Here, we are reading the information about 100 books from the user. After reading, we are storing it into the structure variable.
Also Read: Arrays in C for Complete Beginners
Instead of declaring 100 separate variables, we have created an array of structure variables of size 100. It is possible to display the output here because we are dealing with the 100 books. But as an example, I can show the output for one book. This will help us to understand the flow of this program.
Output (C Program to Store and Display Data of 1 Book)
Enter Data of Book 1
Title: C Programming
Author: E. Balagurusamy
Publication: TMH
Price: 345
Book 1
Title: C Programming
Author: E. Balagurusamy
Publication: TMH
Price: 345.00
From this output, we can get clear idea how we are reading, storing and displaying the information of the 100 books. Actual output of this program will read the data of 100 books and after that, it will display the same information of the 100 books.
I hope you will like this program. If you have any doubts or any feedback then please let me know.
Thank you.