Sem-2 Practical Practice Question1

Write a C program that creates structure student with sid, sname, sem,per(%). Take input of 5 students and display those student’s name who has achieved more than 70%.

SOLUTION

#include stdio.h>
#include string.h>

#define SIZE 5

// Define the student structure
struct student {
    int sid;
    char sname[50];
    int sem;
    float per;
};

int main() {
    struct student s[SIZE];

    // Input details of 5 students
    for (int i = 0; i < SIZE; i++) {
	printf("\nEnter details for Student %d\n", i + 1);
	printf("Student ID: ");
	scanf("%d", &s[i].sid);
	getchar(); // clear newline
	printf("Student Name: ");
	fgets(s[i].sname, sizeof(s[i].sname), stdin);
	// Remove newline character from name
	if (s[i].sname[strlen(s[i].sname) - 1] == '\n') {
	    s[i].sname[strlen(s[i].sname) - 1] = '\0';
	}
	printf("Semester: ");
	scanf("%d", &s[i].sem);
	printf("Percentage: ");
	scanf("%f", &s[i].per);
    }

    // Display students with percentage more than 70%
    printf("\nStudents who scored more than 70%%:\n");
    for ( i = 0; i < SIZE; i++) {
	if (s[i].per > 70) {
	    printf("%s (%.2f%%)\n", s[i].sname, s[i].per);
	}
    }

    return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *