C bootcamp · Lab 13

Letter grade

easyIf Else10 minLesson: If Else

Read the question, write C on the right, then Run or Check.

QuestionHint and solution stay closed until you open them

Read a score from 0 to 100 and print its letter grade.

90+ is A, 80–89 B, 70–79 C, 60–69 D, and below 60 is F.

Input. One line: an integer 0–100.

Output. A single letter: A, B, C, D, or F.

Constraints

  • 0 ≤ score ≤ 100

Examples

Example 1
Input
95
Output
A
Example 2
Input
82
Output
B
Example 3 — Boundaries matter: 59 fails, 60 passes.
Input
59
Output
F
Hint
  1. Check the highest band first with an if / else if chain.
  2. Once you have checked score >= 90, the next test only needs score >= 80.
Show correct code

Peek only after you have tried. You can still Check your own version.

#include <stdio.h>

int main(void) {
  int score;
  scanf("%d", &score);
  if (score >= 90) printf("A\n");
  else if (score >= 80) printf("B\n");
  else if (score >= 70) printf("C\n");
  else if (score >= 60) printf("D\n");
  else printf("F\n");
  return 0;
}
main.cC17 · gcc · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.