# Java Program to Calculate and Display Student Grades – Explained

In this Java tutorial, we will learn to develop a Java Program to calculate Student Grades. We will see, How to calculate Student Grades programmatically and also understand the working logic behind the Java Program to calculate the Student Grades. So, Let’s get started-

Things, you should know

Before we actually start the solution of the Student Grade Calculation problem, for which you should have a basic knowledge of these Core Java Concepts applied in this tutorial –

• Variable declaration and definition,
• Loops in Java (for / while)
• Defining custom methods

## 1) Solution – Java Program Calculate Student Grades

Its a simple program that will tell you the average Grade scored by the student. Here is a simple solution to calculate and display Student’s Average Grades using Java –

```package com.shubhamklogic.java.programs;

/*
Java Program : To calculate average Grade of a Student
By : ShubhamKLogic.com  ||  Visit for more Java concepts & Solutions ...
*/
import java.util.Scanner;                                                    // Explaining : Step-by-Step

public class JavaStudentGradeSolution1 {                      // The body of the Class - Started

public static void main(String[] theArgs) {                // main method

/*            This solution will work for 1 Student who has
5 subjects, And you need to Display the Grade.
However, You can update it, as per you choice.
*/

final int numberOfSubjects = 5;                               // Total number Of Subjects
int theMarks[] = new int[numberOfSubjects];     // Creating an Array to store Marks
int i;
float total = 0, theAverage;

Scanner scanner = new Scanner(System.in);    // Scanner will take Input from User

for (i = 0; i < numberOfSubjects; i++) {                  // Looping through all Subjects
System.out.print("Enter the Marks of Subject (" + (i + 1) + ") :");
theMarks[i] = scanner.nextInt();
total = total + theMarks[i];
}

scanner.close();

// Now, Calculating and Printing theGrade
theAverage = total / numberOfSubjects;

if (theAverage >= 80) {
System.out.print("A");                                                 // Displaying the Calculated Grade -
}
else if (theAverage < 80 && theAverage >= 60) {
System.out.print("B");
}
else if (theAverage < 60 && theAverage >= 40) {
System.out.print("C");
}
else {
System.out.print("D");
}
}
}
```

### Output :

When you’ll run the above Java program, it will successfully calculate and print Grades of Students on the basis of input marks –

• First Run :
```Enter the Marks of Subject (1) : 44
Enter the Marks of Subject (2) : 55
Enter the Marks of Subject (3) : 77
Enter the Marks of Subject (4) : 88
Enter the Marks of Subject (5) : 99
```

• Second Run :
```Enter the Marks of Subject (1) : 76
Enter the Marks of Subject (2) : 86
Enter the Marks of Subject (3) : 98
Enter the Marks of Subject (4) : 88
Enter the Marks of Subject (5) : 99

## What’s the Logic – Behind the Code?

The logic of the above Java program to Calculate Student Grades is pretty simple. To understand it better, you just follow these major steps –

[Step 1] Define a class and main method, (package is optional),

[Step 2] To calculate the Grade, you must have their marks. That’s why, we set the numberOfSubjects, and,

[Step 3] Asked the user to Input theMarks.

[Step 4] Then, we need to calculate the total of theMarks for getting theAverage.

[Step 5] After calculating theAverage, we need to define the range of Grading.

[Step 6] Here, in the above Student Grade Calculator Java Program, we have given Grade A to students having theAverage marks greater than or equal to 80.

[Step 7] Similarly, Grade B to student having theAverage less than 80 but greater than or equal to 60

[Step 8] The student will get Grade C if theAverage is below 60 and greater than or equal to 40

[Step 9] And Grade D to all those students who have scored theAverage below 40.

## 2) Solution : Java program to Find Grade of N students

Here is a solution of the above N Students Grade Calculation problem –

```import java.util.Scanner;                                                   // Explaining : Step-by-Step

public class JavaStudentGradeSolution2{                    // The body of the Class - Started

public static void main(String[] theArgs) {             // main method

final int numberOfSubjects = 5;
int theMarks[] = new int[numberOfSubjects];

int i, numberOfStudents = 0;                                      // ADDED to store number of Students
float total = 0, theAverage;

boolean moreStudents = true;                                   // ADDED to ask the user - if the user has more student's details to

Scanner scanner = new Scanner(System.in);
while (moreStudents) {                                               // ADDED while loop, To calculate Grades untill user has more Students' details

numberOfStudents++;                                             // ADDED the variable numberOfStudents to store- Till where, the List is done.

printPretty("Details: Student " + numberOfStudents);       // ADDED our custom printPretty( ) method

for (i = 0; i < numberOfSubjects; i++) {                              // Taking marks of all Subjects
System.out.print("Enter the Marks of Subject (" + (i + 1) + ") :");
theMarks[i] = scanner.nextInt();
total = total + theMarks[i];
}

System.out.println("Total = " + total);
theAverage = total / numberOfSubjects;

}
else if (theAverage < 80 && theAverage >= 60) {
}
else if (theAverage < 60 && theAverage >= 40) {
}
else {
}

System.out.print("Do You Have More Students ??? (Yes-> 1 , No-> 0) \n\nEnter Choice (Press Key) -> ");

char usersChoice = scanner.next().charAt(0);             // ADDED  to ask users choice, whether the user has more students or not?

if (usersChoice != '1') {                                                    // ADDED to set the flag 'moreStudents' with 'false' value to quit the while loop

printPretty("Total Students = " + numberOfStudents);
moreStudents = false;
System.out.println("Application is Shutting Down...");

} else {

total = 0;
theAverage = 0;

}
}
System.out.println("Application Closed !!!");
}

public static void printPretty(String msg) {

System.out.print("\n\n***************************************************\n");
System.out.print(     "******************  " + msg + "  ***********\n");
System.out.print(     "***************************************************\n\n\n");

}
}
```

### Output

When you’ll run the above program, you would successfully get the Output, like –

```C:\Users\ShubhamKLogic_com>javac  JavaStudentGradeSolution2.java

************************************************
******************  Details: Student 1  ***********
************************************************

Enter the Marks of Subject (1) : 85
Enter the Marks of Subject (2) : 90
Enter the Marks of Subject (3) : 85
Enter the Marks of Subject (4) : 90
Enter the Marks of Subject (5) : 85
Total = 435.00

************************************************
******************  Student 1: Grade A  ***********
************************************************

Do You Have More Students ??? (Yes-> 1 , No-> 0)

Enter Choice (Press Key) -> 1

************************************************
******************  Details: Student 2  ***********
************************************************

Enter the Marks of Subject (1) : 75
Enter the Marks of Subject (2) : 70
Enter the Marks of Subject (3) : 75
Enter the Marks of Subject (4) : 70
Enter the Marks of Subject (5) : 75
Total = 365.00

************************************************
******************  Student 2: Grade B  ***********
************************************************

Do You Have More Students ??? (Yes-> 1 , No-> 0)

Enter Choice (Press Key) -> 1

************************************************
******************  Details: Student 3  ***********
************************************************

Enter the Marks of Subject (1) : 55
Enter the Marks of Subject (2) : 50
Enter the Marks of Subject (3) : 55
Enter the Marks of Subject (4) : 50
Enter the Marks of Subject (5) : 55
Total = 265.00

************************************************
******************  Student 3: Grade C  ***********
************************************************

Do You Have More Students ??? (Yes-> 1 , No-> 0)

Enter Choice (Press Key) -> 1

************************************************
******************  Details: Student 4  ***********
************************************************

Enter the Marks of Subject (1) : 33
Enter the Marks of Subject (2) : 30
Enter the Marks of Subject (3) : 33
Enter the Marks of Subject (4) : 30
Enter the Marks of Subject (5) : 30
Total = 156.00

************************************************
******************  Student 4: Grade D  ***********
************************************************

Do You Have More Students ??? (Yes-> 1 , No-> 0)

Enter Choice (Press Key) -> 0

************************************************
******************  Total Students = 4  ***********
************************************************

Application is Shutting Down . . .
Application Closed !!!```

## How we Enhanced – Student Grade Calculation Program

In the above Java program to find Grade of N students, we have basically declared a boolean type variable moreStudents = true; and ran a while loop until the user has given the Input 0 (zero) to close the application.

If you want to understand all the major cross-cutting points of the above solution in comparison to the 1st Student Grade Calculator solution, check the comments having ADDED keywords in above Java program.

## Wrap Up

So far, we have developed the Student Grade Calculator Java Program. We also understood the working logic behind the code in just 9 Steps. However, these solutions can be further optimized for Robustness and better performance.

I hope, this article helped you to learn a bit more about Java Arithmetics, Calculations and Conditional block concepts. If you have any doubt or query, then do comment or send me a direct email via Google, I’ll try my best to give you the solution within a day.

Also, Rate this article on Facebook and, Don’t forget to check Other Useful Java Articles that you may like :