Java Program to Find Grade from Marks
You can find grade from marks in Java using if-else statements like
if (marks >= 90) grade = 'A'; else if (marks >= 80) grade = 'B'; and so on, to assign grades based on score ranges.Examples
Inputmarks = 95
OutputGrade: A
Inputmarks = 72
OutputGrade: C
Inputmarks = 40
OutputGrade: F
How to Think About It
To find the grade from marks, first understand the score ranges for each grade. Then check the marks against these ranges using
if and else if conditions from highest to lowest. Assign the grade when the marks fit a range.Algorithm
1
Get the marks as input.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
java
import java.util.Scanner; public class GradeFinder { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter marks: "); int marks = sc.nextInt(); char grade; 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'; System.out.println("Grade: " + grade); sc.close(); } }
Output
Enter marks: 85
Grade: B
Dry Run
Let's trace marks = 85 through the code
1
Input marks
marks = 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 | none |
| marks >= 80 | true | B |
Why This Works
Step 1: Check highest grade first
We start with if (marks >= 90) to assign grade 'A' because higher marks get higher grades.
Step 2: Use else if for next ranges
Each else if checks the next lower range, so only one grade is assigned.
Step 3: Assign default grade
If none of the above conditions match, grade 'F' is assigned as the default for failing marks.
Alternative Approaches
Using switch with ranges (Java 14+)
java
import java.util.Scanner; public class GradeFinderSwitch { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter marks: "); int marks = sc.nextInt(); char grade; 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'; } System.out.println("Grade: " + grade); sc.close(); } }
This uses integer division and switch-case for cleaner code but only works well for marks 0-100.
Using ternary operator
java
import java.util.Scanner; public class GradeFinderTernary { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.print("Enter marks: "); int marks = sc.nextInt(); char grade = (marks >= 90) ? 'A' : (marks >= 80) ? 'B' : (marks >= 70) ? 'C' : (marks >= 60) ? 'D' : 'F'; System.out.println("Grade: " + grade); sc.close(); } }
This approach is concise but can be harder to read for beginners.
Complexity: O(1) time, O(1) space
Time Complexity
The program uses simple if-else checks without loops, so it runs in constant time regardless of input.
Space Complexity
Only a few variables are used, so space usage is constant.
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) | Clear and easy to understand |
| 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 marks range first to assign the correct grade.
Beginners often check lower ranges first, causing incorrect grade assignments.