Sem-2 Practical Practice Question2
Q.2 Write a program that creates structure of student with student id, name, percentage. Enter data of five students. Display data of those student whose percentage is less than 70 marks
SOLUTION.
#include stdio.h>
#include string.h>
// Define structure
struct Student {
int id;
char name[50];
float percentage;
};
int main() {
struct Student students[5];
// Input data for 5 students
for (int i = 0; i < 5; i++) {
printf("\nEnter details for Student %d:\n", i + 1);
printf("Student ID: ");
scanf("%d", &students[i].id);
getchar(); // consume newline after int input
printf("Name: ");
fgets(students[i].name, sizeof(students[i].name), stdin);
if (students[i].name[strlen(students[i].name) - 1] == '\n')
students[i].name[strlen(students[i].name) - 1] = '\0'; // Remove newline
printf("Percentage: ");
scanf("%f", &students[i].percentage);
}
// Display students with percentage < 70
printf("\nStudents with percentage less than 70%%:\n");
for (int i = 0; i < 5; i++) {
if (students[i].percentage < 70) {
printf("\nStudent ID : %d", students[i].id);
printf("\nName : %s", students[i].name);
printf("\nPercentage : %.2f\n", students[i].percentage);
}
}
return 0;
}
