AppXwinD Learn
← Previous Program 1 of 1 Next →

Student structure and sorting based on marks

Structure
Easy • 0 votes

📝 Question

Consider a structure as follow:
struct stud_info
{
 int rno;
 char sname[30];
 int marks;
};
Write a C program that accept details of N-students and display student's details in descending order on marks.
✅ Solution:
#include <stdio.h>

struct stud_info
{
    int rno;
    char sname[30];
    int marks;
};

int main()
{
    struct stud_info s[50], temp;
    int n, i, j;

    printf("Enter number of students: ");
    scanf("%d", &n);

    // Input student details
    for(i = 0; i < n; i++)
    {
        printf("\nEnter details of student %d\n", i + 1);

        printf("Roll No: ");
        scanf("%d", &s[i].rno);

        printf("Name: ");
        scanf("%s", s[i].sname);

        printf("Marks: ");
        scanf("%d", &s[i].marks);
    }

    // Sorting in descending order of marks
    for(i = 0; i < n - 1; i++)
    {
        for(j = i + 1; j < n; j++)
        {
            if(s[i].marks < s[j].marks)
            {
                temp = s[i];
                s[i] = s[j];
                s[j] = temp;
            }
        }
    }

    // Display sorted student details
    printf("\nStudent Details in Descending Order of Marks:\n");
    printf("\nRoll No\tName\tMarks\n");

    for(i = 0; i < n; i++)
    {
        printf("%d\t%s\t%d\n", s[i].rno, s[i].sname, s[i].marks);
    }

    return 0;
}
💬 Q&A (0)
← Previous Back to List Next →