Book Example With Total Rate


Explanation

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?

  • Step 1: Create a structure named Book with members name, pages, and rate.
  • Step 2: Create an array of structure b[5] to store multiple book records.
  • Step 3: Accept details of three books using a for loop.
  • Step 4: Display all stored book records using another for loop.
  • Step 5: Add the rate of each book and store the total in the variable total.
  • Step 6: Display the total rate of all books.

Example Program


#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();

}

Output

Enter the Book Name : C
Enter the Book Page : 150
Enter the Rate of Book : 250

Enter the Book Name : CPlusPlus
Enter the Book Page : 200
Enter the Rate of Book : 400

Enter the Book Name : Java
Enter the Book Page : 300
Enter the Rate of Book : 500

Name    Pages   Rate
C         150     250
CPlusPlus  200     400
Java       300     500

Total of books rate is 1150

Practice Exercises

Task 1: Write a C program using an array of structure to store details of 5 products and calculate the total price of all products.

Goal: Learn how to use an array of structures with calculations by accessing structure members.

💡 Hint: Create a structure named 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.
💡 Show Solution

#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();

}

Output
Enter Product Name : Pen
Enter Quantity : 10
Enter Price : 100

Enter Product Name : Book
Enter Quantity : 5
Enter Price : 500

Enter Product Name : Bag
Enter Quantity : 2
Enter Price : 800

Name    Quantity   Price
Pen       10        100
Book     5         500
Bag       2         800

Total Price is 1400
Explanation: The program creates a structure named 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.