C: Letter grade

Read marks out of 100 and print the grade:

MarksGrade
80 to 100A+
70 to 79A
60 to 69B
50 to 59C
40 to 49D
0 to 39F

Print invalid for marks below 0 or above 100.

What to build

A chain of conditions with seven possible results.

Requirements

  • Check for invalid marks first.
  • Print the grade from the table above.

Input

One whole number.

Output

The grade, or invalid.

Constraints

The marks can be any whole number that fits in an int.

Example

Input:

85

Output:

A+

Why it works

Once the invalid marks are excluded, testing from the highest band downward means each test need only give the lower limit.

Examples

Example 1 · Input
85
Expected output
A+
Example 2 · Input
39
Expected output
F

Hints

Hint 1

Check from the highest band down with else if.

Hint 2

Marks outside 0 to 100 should print invalid; test that first.

Solution

Show one correct solution (try it yourself first)
#include <stdio.h>

int main(void) {
    int marks;
    scanf("%d", &marks);
    if (marks < 0 || marks > 100) printf("invalid\n");
    else if (marks >= 80) printf("A+\n");
    else if (marks >= 70) printf("A\n");
    else if (marks >= 60) printf("B\n");
    else if (marks >= 50) printf("C\n");
    else if (marks >= 40) printf("D\n");
    else printf("F\n");
    return 0;
}

Lesson: Decisions: if, else and switch →