C Program to Find Grade from Marks
Use a C program with
if-else statements to check marks and print grades, for example: if(marks >= 90) printf("Grade A"); else if(marks >= 80) printf("Grade B"); and so on.Examples
Inputmarks = 95
OutputGrade A
Inputmarks = 75
OutputGrade C
Inputmarks = 40
OutputGrade F
How to Think About It
To find the grade from marks, first get the marks as input. Then compare the marks with fixed ranges using
if and else if conditions. Assign the grade based on which range the marks fall into, for example, 90 and above is A, 80 to 89 is B, and so on.Algorithm
1
Get the marks input from the user2
Check if marks are greater than or equal to 90, assign grade A3
Else if marks are greater than or equal to 80, assign grade B4
Else if marks are greater than or equal to 70, assign grade C5
Else if marks are greater than or equal to 60, assign grade D6
Else assign grade F7
Print the gradeCode
c
#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); if (marks >= 90) { printf("Grade A\n"); } else if (marks >= 80) { printf("Grade B\n"); } else if (marks >= 70) { printf("Grade C\n"); } else if (marks >= 60) { printf("Grade D\n"); } else { printf("Grade F\n"); } return 0; }
Output
Enter marks: 85
Grade B
Dry Run
Let's trace the program with marks = 85 through the code
1
Input marks
User enters 85, so marks = 85
2
Check if marks >= 90
85 >= 90 is false, so skip Grade A
3
Check if marks >= 80
85 >= 80 is true, so print Grade B
4
End program
Program prints Grade B and ends
| Step | Condition Checked | Result | Grade Printed |
|---|---|---|---|
| 1 | marks >= 90 | false | |
| 2 | marks >= 80 | true | Grade B |
Why This Works
Step 1: Input marks
The program asks the user to enter marks and stores it in a variable.
Step 2: Check conditions
It uses if-else if to compare marks with grade ranges in descending order.
Step 3: Print grade
When a condition matches, the corresponding grade is printed and the program ends.
Alternative Approaches
Using switch with ranges (requires integer division)
c
#include <stdio.h> int main() { int marks, grade; printf("Enter marks: "); scanf("%d", &marks); grade = marks / 10; switch(grade) { case 10: case 9: printf("Grade A\n"); break; case 8: printf("Grade B\n"); break; case 7: printf("Grade C\n"); break; case 6: printf("Grade D\n"); break; default: printf("Grade F\n"); } return 0; }
This method uses integer division and switch-case for cleaner code but is less flexible for non-standard ranges.
Using ternary operator
c
#include <stdio.h> int main() { int marks; printf("Enter marks: "); scanf("%d", &marks); char grade = (marks >= 90) ? 'A' : (marks >= 80) ? 'B' : (marks >= 70) ? 'C' : (marks >= 60) ? 'D' : 'F'; printf("Grade %c\n", grade); 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 regardless of input size, so it runs in constant time O(1).
Space Complexity
It uses a few variables and no extra data structures, so space complexity is O(1).
Which Approach is Fastest?
All approaches run in constant time; switch-case may be slightly faster but less flexible than if-else.
| Approach | Time | Space | Best For |
|---|---|---|---|
| If-else | O(1) | O(1) | Simple and clear grading logic |
| Switch-case | O(1) | O(1) | Cleaner code with fixed ranges |
| Ternary operator | O(1) | O(1) | Concise code, less readable for beginners |
Always check the highest grade condition first to avoid incorrect grading.
Beginners often check lower grades first, causing all marks to be assigned the lowest grade.