Java program to calculate grades – Java Program to Calculate Grade of Students

Java program to calculate grades: Access the Simple Java program for Interview examples with output from our page and impress your interviewer panel with your coding skills.

Java Program to Calculate Grade of Students

  • Java Program to find the grade of a student, given the marks of N subjects.

Given the marks of N subjects, we have to print the grade of a student based on the following grade slab.

  • If Percentage Marks > 90, Grade is A+
  • If 70 <= Percentage Marks <= 89, Grade is A
  • If 60 <= Percentage Marks <= 69, Grade is B
  • If 50 <= Percentage Marks <= 59, Grade is C
  • If Percentage Marks <= 40, Grade is D

In this java program, we first ask user to enter number of subjects and store it in variable “count”. Then using a for loop, we take marks of “count” subjects as input from user and add them to variable “totalMarks”. Then we find the percentage marks of student using following expression assuming each subject is of 100 marks.

percentage = (totalMarks/(count*100)) * 100;
Using a switch case, we check the grade of the student as per the slab mentioned above and print it on screen.

Java program to calculate the grade of a student

Java program to calculate the grade of a student

package com.tcc.java.programs;
 
import java.util.Scanner;
 
/**
 * Java Program to print grade of a student
 */
public class StudentGrade {
    public static void main(String[] args) {
        int count, i;
        float totalMarks = 0, percentage, average;
        Scanner scanner;
        scanner = new Scanner(System.in);
 
        System.out.println("Enter Number of Subject");
        count = scanner.nextInt();
 
        System.out.println("Enter Marks of " + count + " Subject");
        for (i = 0; i < count; i++) {
            totalMarks += scanner.nextInt();
        }
        System.out.println("Total MArks : " + totalMarks);
        // Each subject is of 100 Marks
        percentage = (totalMarks / (count * 100)) * 100;
 
        /* Printing grade of a student using switch case statement */
        switch ((int) percentage / 10) {
        case 9:
            System.out.println("Grade : A+");
            break;
        case 8:
        case 7:
            System.out.println("Grade : A");
            break;
        case 6:
            System.out.println("Grade : B");
            break;
        case 5:
            System.out.println("Grade : C");
            break;
        default:
            System.out.println("Grade : D");
            break;
        }
    }
}

Output

Enter Number of Subject
5
Enter Marks of 5 Subject
45 69 53 58 62
Total MArks : 287.0
Grade : C