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?
Book with members name, pages, and rate.b[5] to store multiple book records.for loop.for loop to check each book's rate.#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(); }
array of structure to store details of 5 students and display only those students whose marks are greater than 75.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.
#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(); }
if condition. Only students having marks greater than 75 are displayed.