Java Program to Find Student Grade

Write a Java program to find student Grade with example. For this, first, we have to calculate the Total and Percentage of given Subjects.

Java Program to Find Student Grade Example

This program helps the user to enter five different integer values for five subjects. Next, it finds the Total and Percentage of those Five Subjects.

For this, we are using the Java Arithmetic Operators to perform arithmetic operations. Next, we used the Java Else If statement to display the Grade.

import java.util.Scanner;

public class StudentGrade {
private static Scanner sc;
public static void main(String[] args)
{
int english, chemistry, computers, physics, maths;
float total, Percentage;
sc = new Scanner(System.in);

System.out.print(" Please Enter the Five Subjects Marks : ");
english = sc.nextInt();
chemistry = sc.nextInt();
computers = sc.nextInt();
physics = sc.nextInt();
maths = sc.nextInt();

total = english + chemistry + computers + physics + maths;
Percentage = (total / 500) * 100;

System.out.println(" Total Marks = " + total);
System.out.println(" Marks Percentage = " + Percentage);

if(Percentage >= 90)
{
System.out.println("\n Grade A");
}
else if(Percentage >= 80)
{
System.out.println("\n Grade B");
}
else if(Percentage >= 70)
{
System.out.println("\n Grade C");
}
else if(Percentage >= 60)
{
System.out.println("\n Grade D");
}
else if(Percentage >= 40)
{
System.out.println("\n Grade E");
}
else
{
System.out.println("\n Fail");
}
}
}
Java Program to Find Student Grade 1

Program for Student Grade using functions

This Java program is the same as above. But this time, we are creating a separate method to display the student grades.

import java.util.Scanner;

public class StudentGrade1 {
private static Scanner sc;
public static void main(String[] args)
{
int english, chemistry, computers, physics, maths;

sc = new Scanner(System.in);

System.out.print(" Please Enter the Five Subjects Marks : ");
english = sc.nextInt();
chemistry = sc.nextInt();
computers = sc.nextInt();
physics = sc.nextInt();
maths = sc.nextInt();

studentGrade(english, chemistry, computers, physics, maths);
}
public static void studentGrade(int eng, int chem, int com, int phy, int math)
{
float total, Percentage;

total = eng + chem + com + phy + math;
Percentage = (total / 500) * 100;

System.out.println(" Total Marks = " + total);
System.out.println(" Marks Percentage = " + Percentage);

if(Percentage >= 90)
{
System.out.println("\n Grade A");
}
else if(Percentage >= 80)
{
System.out.println("\n Grade B");
}
else if(Percentage >= 70)
{
System.out.println("\n Grade C");
}
else if(Percentage >= 60)
{
System.out.println("\n Grade D");
}
else if(Percentage >= 40)
{
System.out.println("\n Grade E");
}
else
{
System.out.println("\n Fail");
}
}
}

The student grades output

 Please Enter the Five Subjects Marks : 80 80 70 85 94
 Total Marks =  409.0
 Marks Percentage =  81.8

 Grade B

Program to find the student grade using switch case

Instead of using the complex else if ladder, we can use a switch statement with multiple cases to find the student grade. Refer to the Java switch case and Java break statement.

import java.util.Scanner;

public class example {
public static void main(String[] args) {
int marks[] = new int[5];
float total = 0, Percentage;
Scanner sc = new Scanner(System.in);
System.out.print("Enter Five Subjects Marks : ");
for (int i = 0; i < 5; i++) {
marks[i] = sc.nextInt();
total += marks[i];
}
sc.close();
Percentage = (total / 500) * 100;

System.out.println("Total Marks = " + total);
System.out.println("Marks Percentage = " + Percentage);

switch ((int) Percentage / 10) {
case 10:
case 9:

System.out.println("A Grade");
break;
case 8:
case 7:
System.out.println("B Grade");
break;
case 6:
case 5:
case 4:
System.out.println("C Grade");
break;
default:
System.out.println("Fail");
}
}
}
Enter Five Subjects Marks : 90 80 80 90 92
Total Marks =  432.0
Marks Percentage =  86.4
B Grade

Also Read