AlgoMaster Logo

Grade from Marks

Last Updated: June 7, 2026

easy
2 min read

Understanding the Problem

This is a range lookup. A score is a single number, but grades come in bands, and the job is to find which band the score falls into. The bands do not overlap and together cover every possible score, so each input maps to exactly one grade.

The bands are ordered, which is why an else-if ladder fits. Once you know a score is not an A, you know it is below 90. Checking the highest band first and working downward means every comparison after the first needs a single threshold, because the upper bound of each band is already guaranteed by the branches that failed before it.

Key Constraints:

  • Marks range from 0 to 100 → You do not need to validate out-of-range inputs, which keeps the focus on the band boundaries.
  • Bands are contiguous and non-overlapping → Each score belongs to exactly one grade, so the first matching branch is always the correct one.
  • Boundaries are inclusive at the lower end → A score of exactly 90 is an A and exactly 60 is a D, so use >= at each threshold.

Approach 1: Else-If Ladder

Intuition

Test the thresholds 90, 80, 70, 60 in that order, and the first one the score clears determines the grade. A score of 83 fails the >= 90 test, then passes the >= 80 test, so it stops there as a B.

If you checked from the bottom up instead, you would write two-sided ranges like marks >= 80 && marks < 90, which is more code and more chances for an off-by-one mistake.

Algorithm

  1. If marks is at least 90, return "A".
  2. Otherwise, if marks is at least 80, return "B".
  3. Otherwise, if marks is at least 70, return "C".
  4. Otherwise, if marks is at least 60, return "D".
  5. Otherwise, return "F".

Example Walkthrough

Input:

72
marks

The score is 72. The first test, 72 >= 90, is false, so it is not an A. The second test, 72 >= 80, is also false. The third test, 72 >= 70, is true, so the ladder stops here and returns "C". The lower branches for D and F never run.

0
C
output

Code