Grade by Score
Programming Basics · C++
TASK
Problem
Given an integer score from 0 to 100, print `A` for scores at least 90, `B` for at least 75, `C` for at least 60, and `D` otherwise.
EXAMPLE
Example
95
A
LIMITS
Constraints
0 ≤ score ≤ 100.
LEARN
Theory for this problem
+
An `if` / `else if` / `else` chain checks alternatives from top to bottom. Put more specific or higher-priority conditions first.
Connection to “Grade by Score”: here, branching selects a case using a Boolean condition; test order matters when cases overlap.
NEED HELP?
Hints
+
['List the possible cases and make them mutually exclusive.', 'After solving “Grade by Score”, verify the algorithm on your own small example and on an allowed boundary case. Print only the required result with no extra text.']
ANSWER
Solution
+
#include <iostream>
#include <algorithm>
using namespace std;
int main() {
long long n;
cin >> n;
if (n >= 90) cout << 'A';
else if (n >= 75) cout << 'B';
else if (n >= 60) cout << 'C';
else cout << 'D';
cout << '\n';
return 0;
}