Skip to main content

Design a structure student_records to contain Roll_no, Name, City and Percentage obtained. Develop a program to read data for 5 students and Display them.

Introduction

In this program, we will learn to define a structure type and use it to store data.

Algorithm

  1. Start
  2. Declare a structure type 'struct student_records' that would contain Roll_no, Name, City and Percentage obtained.
  3. Declare an array of 5 elements for the structure.
  4. Loop to read data for 5 students.
  5. Read data for one student.
  6. Print data for 5 students.
  7. Print info for one student.
  8. End

Code

Practical-11.c

//This code is to design a structure student_records to contain
// Roll_no, Name, City and Percentage obtained
//And to then read data for 5 students
//and display them

#include<stdio.h>

// A structure type is defined at the top with
// four fields, Roll_no, Name, City and Percentage
struct student_records {
int Roll_no;
char Name[30];
char City[20];
float Percentage;
};

void main() {
// Declare an array of 5 elements
// for the student_records structure
struct student_records record[5];

// Loop to read data for 5 students
for (int i=0; i<5; i++)
{
// Read data for one student
printf("Enter Roll_no of student %d:\n", i+1);
scanf("%d", &(record[i].Roll_no));
printf("Enter Name of student %d:\n", i+1);
scanf("%s", record[i].Name);
printf("Enter City of student %d:\n", i+1);
scanf("%s", record[i].City);
printf("Enter Percentage of student %d:\n", i+1);
scanf("%f", &(record[i].Percentage));
}

printf("\n-------Here is the information of the students--------\n");

// Loop to print data for 5 students
for (int i=0; i<5; i++)
{
// Print info for one student
printf("Student %d\n", i+1);
printf("Roll_no: %d\n", record[i].Roll_no);
printf("Name: %s\n", record[i].Name);
printf("City: %s\n", record[i].City);
printf("Percentage: %f\n", record[i].Percentage);
}


}

Output

Enter Roll_no of student 1:
11
Enter Name of student 1:
Naruto
Enter City of student 1:
Idar
Enter Percentage of student 1:
90
Enter Roll_no of student 2:
12
Enter Name of student 2:
Itachi
Enter City of student 2:
Himmatnagar
Enter Percentage of student 2:
95
Enter Roll_no of student 3:
13
Enter Name of student 3:
Sasuke
Enter City of student 3:
Himmatnagar
Enter Percentage of student 3:
80
Enter Roll_no of student 4:
14
Enter Name of student 4:
Sakura
Enter City of student 4:
Idar
Enter Percentage of student 4:
30
Enter Roll_no of student 5:
15
Enter Name of student 5:
Hinata
Enter City of student 5:
Idar
Enter Percentage of student 5:
95

-------Here is the information of the students--------

Student 1
Roll_no: 11
Name: Naruto
City: Idar
Percentage: 90.000000
Student 2
Roll_no: 12
Name: Itachi
City: Himmatnagar
Percentage: 95.000000
Student 3
Roll_no: 13
Name: Sasuke
City: Himmatnagar
Percentage: 80.000000
Student 4
Roll_no: 14
Name: Sakura
City: Idar
Percentage: 30.000000
Student 5
Roll_no: 15
Name: Hinata
City: Idar
Percentage: 95.000000
Explanation

In this program, we have defined a structure type 'struct student_records' that would contain Roll_no, Name, City and Percentage obtained. We have then declared an array of 5 elements for the structure. We have then looped to read data for 5 students and print data for 5 students.