Write C code to read an integer value for a student's mark (0-100) and print the correct letter grade based on the following:
Below 50 – F
50 to 59 – C
60 to 69 – B
70 to 79 – B
80 to 89 – A
90 to 100 - A+

Source Code

#include <stdio.h>

int main(int argc, char **argv) {
    int mark;
    printf("Enter student marks(0-100): ");
    scanf("%d", &mark);
    while(mark < 0 || mark > 100) {
        printf("Invalid mark\n");
        printf("Please enter a valid mark: ");
        scanf("%d", &mark);
    }
    if(mark < 50) printf("F\n");
    if(mark >= 50 && mark <= 59) printf("C\n");
    if(mark >= 60 && mark <= 69) printf("B\n");
    if(mark >= 70 && mark <= 79) printf("B+\n");
    if(mark >= 80 && mark <= 89) printf("A\n");
    if(mark >= 90 && mark <= 100) printf("A+\n");
    return 0;
}

When you compile and execute the above program it produces the following result on Linux:

Enter student marks(0-100): 0
F
Enter student marks(0-100): 50
C
Enter student marks(0-100): 61
B
Enter student marks(0-100): 71
B+
Enter student marks(0-100): 83
A
Enter student marks(0-100): 90
A+

Brief Explanation

  • The program starts by requesting the user to enter a mark between 0-100
  • Using a 'while' loop, the program validates that the input is between 0-100
  • Using 'if' statements, the program prints the correct letter grade if the mark is within any of the acceptable ranges.

Add comment


Security code
Refresh