C++ bootcamp · Lab 05

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 if / else if.
  2. Chained ternaries work too but an if-chain reads clearer here.
Show correct code

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

#include <iostream>

int main() {
  int score;
  std::cin >> score;
  if (score >= 90) std::cout << "A\n";
  else if (score >= 80) std::cout << "B\n";
  else if (score >= 70) std::cout << "C\n";
  else if (score >= 60) std::cout << "D\n";
  else std::cout << "F\n";
  return 0;
}
main.cppC++17 · g++ · Ctrl + Enter runs
ResultIdle
Run to see output. Check grades the tests.