Index « Previous Next »

Question

The marks obtained by a student in 3 different subjects are input by the user. Your program should calculate the average of subjects and display the grade. The student gets a grade as per the following rules:

AverageGrade
90-100A
80-89B
70-79C
60-69D
0-59F

Source Code

#include <stdio.h>

int main()
{
    float marks1, marks2, marks3, average;

    printf("Enter marks obtained in subject 1 :");
    scanf("%f", &marks1);
    printf("Enter marks obtained in subject 2 :");
    scanf("%f", &marks2);
    printf("Enter marks obtained in subject 3 :");
    scanf("%f", &marks3);

    average = (marks1 + marks2 + marks3) / 3;
    printf("Average : %0.2f\n", average);

    if (average >= 90)
    {
        printf("Grade A");
    }
    else if (average >= 80)
    {
        printf("Grade B");
    }
    else if (average >= 70)
    {
        printf("Grade C");
    }
    else if (average >= 60)
    {
        printf("Grade D");
    }
    else
    {
        printf("Grade F");
    }

    return 0;
}

Output

Enter marks obtained in subject 1 :45
Enter marks obtained in subject 2 :58
Enter marks obtained in subject 3 :78
Average : 60.33
Grade D