QUESTION #017

Grade Calculator Based on Marks

Easy

📋 Problem Statement

Based on the given marks, print the corresponding grade according to the following conditions:

Marks Range Grade
marks ≥ 91 Grade A
76 ≤ marks ≤ 90 Grade B
61 ≤ marks ≤ 75 Grade C
marks ≤ 60 Grade D

📥 Input Format

Accept a integer as input

📤 Output Format

Print the output as corresponding grading: "Grade A" or "Grade B" or "Grade C" or "Grade D".

💡 Examples

Example 1:
Input: 76
Output: Grade B
Explanation:
76 falls in the range 76 ≤ marks ≤ 90, so the grade is B.
Example 2:
Input: 95
Output: Grade A
Explanation:
95 is ≥ 91, so the grade is A (highest grade).

⚠️ Constraints

0 <= mark <= 100

✅ Solution

#include <iostream>

using namespace std;

int main() {
    int marks;
    cin >> marks;
    
    if (marks <= 60)
        cout << "Grade D";
    else if (marks >= 61 && marks <= 75)
        cout << "Grade C";
    else if (marks >= 76 && marks <= 90)
        cout << "Grade B";
    else if (marks >= 91 && marks <= 100)
        cout << "Grade A";
    
    return 0;
}

🔑 Key Concepts

  • Else-if Ladder: Sequential checking of multiple mutually exclusive conditions
  • Range-based Classification: Using comparison operators to check if value falls within a range
  • AND Operator (&&): Checking both lower and upper bounds of a range
  • Grading System: Real-world application of conditional logic in education systems
  • Sequential Logic: Order of conditions can affect efficiency but not correctness
  • String Output: Printing formatted grade strings based on conditions

⚠️ Common Mistakes to Avoid

  • Overlapping Ranges: Using marks >= 60 instead of marks >= 61 for Grade C would create overlap
  • Missing Range Checks: Not including upper bound checks can cause incorrect grade assignments
  • Wrong Condition Order: Checking broader conditions before specific ones may cause issues
  • Boundary Values: Be careful with = sign - decide if 60 is Grade C or Grade D
  • Missing else: What happens if marks > 100? Consider adding validation
  • Wrong Operators: Using OR (||) instead of AND (&&) for range checks

💡 Optimization Tip

  • Simplify Conditions: Since we're using else-if, the second condition in each check is redundant
  • Example: else if (marks <= 75) is enough instead of else if (marks >= 61 && marks <= 75)
  • This works because the previous if already eliminated marks ≤ 60
  • More efficient and cleaner code with the same logic
// Optimized version:
if (marks <= 60)
    cout << "Grade D";
else if (marks <= 75)  // Already know marks > 60
    cout << "Grade C";
else if (marks <= 90)  // Already know marks > 75
    cout << "Grade B";
else                      // Already know marks > 90
    cout << "Grade A";

🎯 What You'll Learn

  • How to implement multi-way classification with else-if ladder
  • Working with ranges and boundary conditions
  • Understanding mutually exclusive conditions
  • Applying logical AND operator for range checks
  • Optimizing conditional logic by eliminating redundant checks
  • Real-world grading system implementation