Java Program to Calculate Student Grades

In this tutorial, you will learn how to write a Java program to calculate student grades based on the marks obtained in an examination. We will use conditional statements—such as if-else and switch to calculate and display the student’s grade.

Building a student grading system is a classic beginner-level Java program. It helps you understand important programming concepts such as user input, conditional statements, decision-making, and logical operators.

We will explore different methods to write a Java program to calculate student grades. Let’s start with what student grades are.

What Are Student Grades?

Student grades are a way of representing a student’s academic performance in an examination or assessment. They are assigned based on the marks or percentage obtained by a student and help evaluate their level of achievement.

In most educational systems, grades are represented using letters such as A, B, C, D, and F. Each grade corresponds to a specific range of marks or percentage.

For example, a student who obtains 95 marks out of 100 may receive grade A, while a student who obtains 75 marks may receive grade C, depending on the grading system used.

The following table shows an example of a student grading system:

Marks ObtainedGradePerformance
90–100AExcellent
80–89BVery Good
70–79CGood
60–69DSatisfactory
0–59FFail

We can use this grading system in the example programs to demonstrate how marks can be converted into grades using conditional statements.

In Java programming, we can calculate student grades by accepting marks as input and comparing them with predefined ranges. Based on the conditions satisfied, the program displays the corresponding grade.

For example, if a student obtains 85 marks, the Java program checks the grading conditions and displays grade B.

Java Program to Find Student Grades Using If-Else

Let’s write a Java program that accepts exam marks from the user and displays the corresponding grade using an if-else-if ladder. For this program, we will use the grading criteria outlined in the table above.

Complete Java Program

import java.util.Scanner;
public class StudentGradeCalculator {
    public static void main(String[] args) {
        // Create a Scanner object to take input from the user
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter their marks
        System.out.print("Enter your marks (out of 100): ");
        int marks = scanner.nextInt();
        
        // First, validate if the entered marks are within the valid range (0 to 100)
        if (marks < 0 || marks > 100) {
            System.out.println("Invalid marks! Please enter a value between 0 and 100.");
        } else {
            // Use an if-else-if ladder to evaluate the marks against the grading criteria
            if (marks >= 90) {
                System.out.println("Grade: A (Excellent)");
            } 
            else if (marks >= 80) {
                System.out.println("Grade: B (Very Good)");
            } 
            else if (marks >= 70) {
                System.out.println("Grade: C (Good)");
            } 
            else if (marks >= 60) {
                System.out.println("Grade: D (Satisfactory)");
            } 
            else {
                System.out.println("Grade: F (Fail)");
            }
        }
        
        // Close the scanner to free up system resources
        scanner.close();
    }
}

Output:

Enter your marks (out of 100): 85
Grade: B (Very Good)

Let’s understand how this Java program works.

  • First, we import the Scanner class from the java.util package to accept input from the keyboard.
  • We create a Scanner object named sc to read the input (marks) from the user.
  • We create a local variable named marks that stores the marks entered by the user.
  • The first if condition checks whether the marks are less than 0 or greater than 100. If either condition is true, the program displays an invalid marks message.
  • The else-if ladder checks the marks from the highest grade to the lowest grade. It checks conditions sequentially. Therefore, there is no need to specify the upper limit in every else-if condition.
  • If the marks are 90 or above, the first condition (marks >= 90) is true, the remaining conditions are skipped, and the program displays grade A.
  • If the marks are 80 or above, the second condition (marks >= 80) is true, the remaining conditions are skipped, and the program displays grade B.
  • Similarly, the program checks the conditions for grades C and D.
  • If none of the previous conditions are satisfied, the final else block displays grade F.
  • Finally, we close the Scanner object using the close() method.

Java Program to Calculate Student Grades Using Switch

You can also calculate a student’s grade using the switch statement in Java. In this approach, we divide the exam marks by 10 and use the resulting integer to determine the grade. For example:

  • Marks 95: 95 / 10 = 9
  • Marks 85: 85 / 10 = 8
  • Marks 75: 75 / 10 = 7

Since integer division automatically drops any decimal portion, numbers like 90–99 all evaluate to 9, making it easy to map them to Grade A in the student grade system. Let’s write a Java program to calculate student grades using the switch statement.

Complete Java Program

import java.util.Scanner;
public class StudentGradeSwitch {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner scanner = new Scanner(System.in);
        
        // Prompt the user to enter their marks
        System.out.print("Enter your marks (out of 100): ");
        int marks = scanner.nextInt();
        
        // First, validate if the entered marks are within the valid range (0 to 100)
        if (marks < 0 || marks > 100) {
            System.out.println("Invalid marks! Please enter a value between 0 and 100.");
        } else {
            // Divide the marks by 10 using integer division to map them to cases
            // For example: 95 / 10 = 9, 82 / 10 = 8, 100 / 10 = 10
            int category = marks / 10;
            
            // Use a switch statement to determine the grade based on the category
            switch (category) {
                case 10: // Handles a score of exactly 100
                case 9:  // Handles scores from 90 to 99
                    System.out.println("Grade: A (Excellent)");
                    break;
                    
                case 8:  // Handles scores from 80 to 89
                    System.out.println("Grade: B (Very Good)");
                    break;
                    
                case 7:  // Handles scores from 70 to 79
                    System.out.println("Grade: C (Good)");
                    break;
                    
                case 6:  // Handles scores from 60 to 69
                    System.out.println("Grade: D (Satisfactory)");
                    break;
                    
                default: // Handles failing scores from 0 to 59
                    System.out.println("Grade: F (Fail)");
                    break;
            }
        }
        
        // Close the scanner to free up system resources
        scanner.close();
    }
}

Output:

Enter your marks (out of 100): 100
Grade: A (Excellent)

Let’s understand how the switch statement works in the program:

  • The program divides the user’s input marks by 10 using integer division to map scores into a single-digit category.
  • Since a score of exactly 100 results in 10 when divided by 10, case 10 is placed directly above case 9 without a break statement.
  • Due to switch fall-through behavior, both scores of 100 and scores in the 90–99 range seamlessly trigger the Grade A output.
  • The remaining cases evaluate single-digit values corresponding to their respective ten-point brackets (case 8 for Grade B, case 7 for Grade C, and case 6 for Grade D).
  • If the category does not match any of the specified cases, the default block catches all scores from 0 to 59 and displays Grade F.
  • Finally, the Scanner object is closed using the close() method to free up system resources.

Java Program to Find Student Grade Using Ternary Operator

The ternary operator (?:) is another way to determine a student’s grade in Java. It provides a shorter, more concise alternative to traditional conditional statements.

Let’s write a Java program to find the grade of a student using the ternary (conditional) operator.

Complete Java Program

import java.util.Scanner;
public class StudentGradeTernary {
    public static void main(String[] args) {
        // Create a Scanner object to read input from the user
        Scanner sc = new Scanner(System.in);

        // Prompt the user to enter their exam marks
        System.out.print("Enter your marks: ");
        int marks = sc.nextInt();

        // First, validate if the entered marks are within the valid range (0 to 100)
        if (marks < 0 || marks > 100) {
            System.out.println("Invalid marks! Please enter a value between 0 and 100.");
        }
        else {
            // Use nested ternary operators (?:) as a concise alternative to an if-else ladder
            // It checks conditions from top to bottom and assigns the corresponding grade
            String grade = (marks >= 90) ? "A" :
                           (marks >= 80) ? "B" :
                           (marks >= 70) ? "C" :
                           (marks >= 60) ? "D" : "F";

            // Display the final grade
            System.out.println("Grade: " + grade);
        }

        // Close the scanner to release system resources
        sc.close();
    }
}

Expected Output:

Enter your marks: 78
Grade: C

Let’s understand how the ternary operator works in the program.

Instead of writing a long if-else statement, the ternary operator offers a shorter way to check a condition and choose between two values. When we chain multiple ternary operators together (known as nesting), the program checks conditions one by one, from top to bottom, until it finds a match.

For example, if a student enters 78:

marks >= 90 is false (moves to the next check).
marks >= 80 is false (moves to the next check).
marks >= 70 is true!

Because it found a match, the program stops right there and assigns Grade C.

While the ternary operator makes your code shorter and more compact, the if-else-if ladder is usually much easier for beginners to read, write, and understand.

DEEPAK GUPTA

DEEPAK GUPTA

Deepak Gupta is the Founder of Scientech Easy, a Full Stack Developer, and a passionate coding educator with 8+ years of professional experience in Java, Python, web development, and core computer science subjects. With strong expertise in full-stack development, he provides hands-on training in programming languages and in-demand technologies at the Scientech Easy Institute, Dhanbad.

He regularly publishes in-depth tutorials, practical coding examples, and high-quality learning resources for both beginners and working professionals. Every article is carefully researched, technically reviewed, and regularly updated to ensure accuracy, clarity, and real-world relevance, helping learners build job-ready skills with confidence.