An array of structure can be used to store multiple records of the same type. We can also perform calculations on structure data by accessing individual structure members.
In this program, an array of structure is used to store details of multiple books. The program accepts book name, pages, and rate. After displaying all book details, it calculates the total rate of all books.
How It Works?
Book with members name, pages, and rate.b[5] to store multiple book records.for loop.for loop.total.#include <stdio.h> #include <conio.h> struct Book { char name[20]; int pages, rate; }; main() { struct Book b[5]; int k, total = 0; clrscr(); for(k = 0; k <= 2; k++) { printf("Enter the Book Name : "); scanf("%s", b[k].name); printf("\nEnter the Book Page : "); scanf("%d", &b[k].pages); printf("\nEnter the Rate of Book : "); scanf("%d", &b[k].rate); } printf("\nName \tPages \tRate"); for(k = 0; k <= 2; k++) { printf("\n%s", b[k].name); printf("\t%d", b[k].pages); printf("\t%d", b[k].rate); total = total + b[k].rate; } printf("\nTotal of books rate is %d", total); getch(); }
array of structure to store details of 5 products and calculate the total price of all products.product with members name, quantity, and price. Store multiple product records using an array of structure. Add all product prices and display the total amount.
#include <stdio.h> #include <conio.h> struct product { char name[20]; int quantity, price; }; main() { struct product p[5]; int k, total = 0; clrscr(); for(k = 0; k <= 4; k++) { printf("Enter Product Name : "); scanf("%s", p[k].name); printf("\nEnter Quantity : "); scanf("%d", &p[k].quantity); printf("\nEnter Price : "); scanf("%d", &p[k].price); } printf("\nName \tQuantity \tPrice"); for(k = 0; k <= 4; k++) { printf("\n%s", p[k].name); printf("\t%d", p[k].quantity); printf("\t%d", p[k].price); total = total + p[k].price; } printf("\nTotal Price is %d", total); getch(); }
product to store product details. An array of structures is used to store multiple product records. The program adds the price of each product using a loop and displays the final total amount.