C++ Program to Find Grade from Marks
Use a C++ program with
if-else statements to check marks and print the corresponding grade, for example: if (marks >= 90) grade = 'A'; else if (marks >= 80) grade = 'B'; and so on.Examples
Input95
OutputGrade: A
Input76
OutputGrade: C
Input45
OutputGrade: F
How to Think About It
To find the grade from marks, first understand the grading scale. Then compare the marks against the scale using
if-else conditions starting from the highest grade to the lowest. Assign the grade based on the first condition that matches.Algorithm
1
Get the marks input from the user.2
Check if marks are 90 or above, assign grade A.3
Else if marks are 80 or above, assign grade B.4
Else if marks are 70 or above, assign grade C.5
Else if marks are 60 or above, assign grade D.6
Else assign grade F.7
Print the grade.Code
cpp
#include <iostream> using namespace std; int main() { int marks; char grade; cout << "Enter marks: "; cin >> marks; if (marks >= 90) grade = 'A'; else if (marks >= 80) grade = 'B'; else if (marks >= 70) grade = 'C'; else if (marks >= 60) grade = 'D'; else grade = 'F'; cout << "Grade: " << grade << endl; return 0; }
Output
Enter marks: 85
Grade: B
Dry Run
Let's trace the program with input marks = 85 through the code
1
Input marks
User enters 85
2
Check if marks >= 90
85 >= 90 is false, move to next condition
3
Check if marks >= 80
85 >= 80 is true, assign grade = 'B'
4
Print grade
Output: Grade: B
| Condition | Result | Grade Assigned |
|---|---|---|
| marks >= 90 | false | |
| marks >= 80 | true | B |
Why This Works
Step 1: Input marks
The program reads the marks entered by the user using cin.
Step 2: Check conditions
It uses if-else statements to compare marks starting from the highest grade boundary.
Step 3: Assign grade
Once a condition is true, the corresponding grade is assigned and the rest are skipped.
Step 4: Output grade
The program prints the grade using cout.
Alternative Approaches
Using switch with ranges (C++20 feature)
cpp
#include <iostream> using namespace std; int main() { int marks; char grade; cout << "Enter marks: "; cin >> marks; switch (marks / 10) { case 10: case 9: grade = 'A'; break; case 8: grade = 'B'; break; case 7: grade = 'C'; break; case 6: grade = 'D'; break; default: grade = 'F'; } cout << "Grade: " << grade << endl; return 0; }
This uses integer division and switch-case for cleaner code but only works well for marks 0-100.
Using ternary operator
cpp
#include <iostream> using namespace std; int main() { int marks; cout << "Enter marks: "; cin >> marks; char grade = (marks >= 90) ? 'A' : (marks >= 80) ? 'B' : (marks >= 70) ? 'C' : (marks >= 60) ? 'D' : 'F'; cout << "Grade: " << grade << endl; return 0; }
This approach is concise but can be harder to read for beginners.
Complexity: O(1) time, O(1) space
Time Complexity
The program uses a fixed number of comparisons with no loops, so it runs in constant time O(1).
Space Complexity
Only a few variables are used, so space complexity is constant O(1).
Which Approach is Fastest?
All approaches run in constant time; switch-case may be slightly faster but less flexible.
| Approach | Time | Space | Best For |
|---|---|---|---|
| if-else | O(1) | O(1) | Simple and clear grading logic |
| switch-case | O(1) | O(1) | Cleaner code for fixed ranges |
| ternary operator | O(1) | O(1) | Concise code, less readable |
Always check the highest grade condition first to avoid incorrect grading.
Beginners often check conditions in ascending order, causing wrong grades to be assigned.