C Program to Store and Display Student Information with Grade Calculation

Subject: PPS (Programming for Problem Solving)

Contributed By: Sanjay

Created At: February 3, 2025

Question:


Write a C Program to Store and Display Student Information with Grade Calculation.

Explanation Video:

Custom Image

Explanation:

 

  • This program stores the name, roll number, year, and marks of three subjects for n students.
  • It calculates the average marks and assigns a grade based on the average.
  • Finally, it displays student details, including name, roll number, average marks, and grade.

Grading Criteria:

Average Marks

Grade

≥ 90

A

80 - 89

B

70 - 79

C

60 - 69

D

< 60

F

Source Code:
#include <stdio.h>

struct Student {
    char name[50];
    int roll_no;
    int year;
    float marks[3];  // Stores marks of 3 subjects
};

char calculateGrade(float avg) {
    if (avg >= 90) return 'A';
    else if (avg >= 80) return 'B';
    else if (avg >= 70) return 'C';
    else if (avg >= 60) return 'D';
    else return 'F';
}

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

    struct Student students[n];

    for (int i = 0; i < n; i++) {
        printf("\nEnter details for student %d\n", i + 1);
        printf("Name: ");
        scanf("%s", students[i].name);
        printf("Roll Number: ");
        scanf("%d", &students[i].roll_no);
        printf("Year: ");
        scanf("%d", &students[i].year);

        printf("Enter marks of 3 subjects: ");
        for (int j = 0; j < 3; j++) {
            scanf("%f", &students[i].marks[j]);
        }
    }

    printf("\nStudent Details with Average and Grade:\n");
    for (int i = 0; i < n; i++) {
        float total = 0;
        for (int j = 0; j < 3; j++) {
            total += students[i].marks[j];
        }
        float avg = total / 3;
        char grade = calculateGrade(avg);

        printf("\nName: %s\nRoll Number: %d\nYear: %d\n", students[i].name, students[i].roll_no, students[i].year);
        printf("Average Marks: %.2f\nGrade: %c\n", avg, grade);
    }

    return 0;
}
Input:
Enter the number of students: 2

Enter details for student 1
Name: John
Roll Number: 101
Year: 2
Enter marks of 3 subjects: 85 90 88

Enter details for student 2
Name: Alice
Roll Number: 102
Year: 3
Enter marks of 3 subjects: 78 82 75
Output:
Student Details with Average and Grade:

Name: John
Roll Number: 101
Year: 2
Average Marks: 87.67
Grade: B

Name: Alice
Roll Number: 102
Year: 3
Average Marks: 78.33
Grade: C
Share this Article & Support Us:
Status
printf('Loading...');