Calculate Student Percentage and Grade in Java


Write a program to input marks of five subjects Physics, Chemistry, Biology, Mathematics and Computer. Calculate percentage and grade according to following:

  • Percentage >= 90% : Grade A
  • Percentage >= 80% : Grade B
  • Percentage >= 70% : Grade C
  • Percentage >= 60% : Grade D
  • Percentage >= 40% : Grade E
  • Percentage < 40% : Grade F

This Java program takes five subject marks as input from the user and calculates the total and percentage of those marks. It also assigns a grade based on the percentage obtained.

Here is how the program works:

  • It first imports the Scanner class from the java.util package to take input from the user.
  • It then creates a new Scanner object named input.
  • The program prompts the user to enter the marks for each of the five subjects.
  • It reads the input using the nextInt() method of the Scanner class and assigns each input value to a separate variable named m1, m2, m3, m4, and m5.
  • It calculates the total marks by adding all the input values together and assigns it to a variable named tot.
  • It calculates the percentage by dividing the total marks by 5 and assigns it to a variable named per.
  • It then prints out the total and percentage values.
  • It checks the value of the percentage and assigns a grade based on the following criteria:
    • If the percentage is greater than or equal to 90, it assigns Grade A.
    • If the percentage is greater than or equal to 80 but less than 90, it assigns Grade B.
    • If the percentage is greater than or equal to 70 but less than 80, it assigns Grade C.
    • If the percentage is greater than or equal to 60 but less than 70, it assigns Grade D.
    • If the percentage is greater than or equal to 40 but less than 60, it assigns Grade E.
    • If the percentage is less than 40, it assigns Grade F.
  • Finally, it prints out the grade assigned to the student based on the percentage obtained.

Note that the program assumes that the user will enter integer values for the marks, and that the percentage calculated will be a whole number. If you want to calculate a more precise percentage, you can change the data type of the variable per to float or double, and use floating-point division instead of integer division in the calculation.

Source Code

import java.util.Scanner;
class Student_Grade
{
	public static void main(String[] args)
	{
		Scanner input = new Scanner(System.in);
		System.out.println("Enter The Five Subject Marks :");
		int m1 = input.nextInt();
		int m2 = input.nextInt();
		int m3 = input.nextInt();
		int m4 = input.nextInt();
		int m5 = input.nextInt();
		int tot = m1+m2+m3+m4+m5;
		float per = tot/5;
		System.out.println("Total :"+tot);
		System.out.println("Percentage :"+per);	
		if(per>=90)
			System.out.println("Grade A");
		else if(per>=80)
			System.out.println("Grade B");
		else if(per>=70)
			System.out.println("Grade C");
		else if(per>=60)
			System.out.println("Grade D");
		else if(per>=40)
			System.out.println("Grade E");
		else
			System.out.println("Grade F");
	}
}

Output

Enter The Five Subject Marks :
65
89
82
95
85
Total :416
Percentage :83.0
Grade B

Example Programs