C Programs | IT Developer
IT Developer

C Programs



Share with a Friend

Structures in C

Find student with highest marks (array of structures)

C Program: Find student with highest marks (array of structures)

C

#include <stdio.h>

 

// Define structure for student

struct Student {

    int rollNo;

    char name[100];

    float marks;

};

 

int main() {

    struct Student s[100];

    int n, i, topIndex = 0;

 

    printf("Enter number of students: ");

    scanf("%d", &n);

    getchar(); // clear input buffer

 

    // Input student details

    for (i = 0; i < n; i++) {

        printf("\nEnter details for Student %d:\n", i + 1);

        printf("Roll No: ");

        scanf("%d", &s[i].rollNo);

        getchar();

 

        printf("Name: ");

        gets(s[i].name);

 

        printf("Marks: ");

        scanf("%f", &s[i].marks);

    }

 

    // Find student with highest marks

    for (i = 1; i < n; i++) {

        if (s[i].marks > s[topIndex].marks)

            topIndex = i;

    }

 

    // Display topper details

    printf("\n===== Student with Highest Marks =====\n");

    printf("Roll No: %d\n", s[topIndex].rollNo);

    printf("Name   : %s\n", s[topIndex].name);

    printf("Marks  : %.2f\n", s[topIndex].marks);

 

    return 0;

}

Output

 
OUTPUT :
Enter number of students: 3

Enter details for Student 1:
Roll No: 101
Name: Ravi Kumar
Marks: 85.5

Enter details for Student 2:
Roll No: 102
Name: Neha Sharma
Marks: 91.2

Enter details for Student 3:
Roll No: 103
Name: Arjun Patel
Marks: 88.7

===== Student with Highest Marks =====
Roll No: 102
Name   : Neha Sharma
Marks  : 91.20

Explanation

Step

Description

struct Student

Holds roll number, name, and marks for each student.

Input Loop

Collects data for all students.

Comparison Loop

Finds the student with the maximum marks.

topIndex

Stores the index of the student having the highest marks.