Book Example With Specific Rate


Explanation

An array of structure can be used to store multiple records. We can apply conditions on structure members to display only specific records according to the requirement.

In this program, multiple book details are stored using an array of structures. The program checks the rate of each book and displays only those books whose rate is greater than 500.

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: Use another for loop to check each book's rate.
  • Step 5: If the book rate is greater than 500, display that book's details.

Example Program


#include <stdio.h>
#include <conio.h>

struct Book
{

    char name[20];

    int pages, rate;

};


main()
{

    struct Book b[5];

    int k;

    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++)

    {

        if(b[k].rate > 500)

        {

            printf("\n%s", b[k].name);

            printf("\t%d", b[k].pages);

            printf("\t%d", b[k].rate);

        }

    }


    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 : 700

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

Name    Pages   Rate
CPlusPlus  200    700
Java      300    900

Practice Exercises

Task 1: Write a C program using an array of structure to store details of 5 students and display only those students whose marks are greater than 75.

Goal: Learn how to apply conditions on structure members and display specific records from an array of structures.

💡 Hint: Create a structure named student with members name, rollno, and marks. Accept multiple student records using an array of structures. Use an if condition to check whether marks > 75 before displaying details.
💡 Show Solution

#include <stdio.h>
#include <conio.h>

struct student
{

    char name[20];

    int rollno, marks;

};


main()
{

    struct student s[5];

    int k;

    clrscr();


    for(k = 0; k <= 4; k++)

    {

        printf("Enter Student Name : ");

        scanf("%s", s[k].name);


        printf("\nEnter Roll No : ");

        scanf("%d", &s[k].rollno);


        printf("\nEnter Marks : ");

        scanf("%d", &s[k].marks);

    }


    printf("\nName \tRoll No \tMarks");


    for(k = 0; k <= 4; k++)

    {

        if(s[k].marks > 75)

        {

            printf("\n%s", s[k].name);

            printf("\t%d", s[k].rollno);

            printf("\t%d", s[k].marks);

        }

    }


    getch();

}

Output
Enter Student Name : Amit
Enter Roll No : 1
Enter Marks : 70

Enter Student Name : Rahul
Enter Roll No : 2
Enter Marks : 85

Enter Student Name : Neha
Enter Roll No : 3
Enter Marks : 90

Name    Roll No   Marks
Rahul    2       85
Neha     3       90
Explanation: The program stores multiple student records using an array of structures. Each student's marks are checked using an if condition. Only students having marks greater than 75 are displayed.