Java Program to Calculate and Print Grade of Students With Output

The program simply calculates and prints grades of a student. It is a basic program, where a user inputs marks for all the subject. Further, it tallies total marks and applies below formula to get percentage figure. Based on the that, we identify grades using conditional operator (if-else).

% = ( Total Marks Obtained / Total Marks ) * 100

Write a program which calculates grade of a student on the basis of entered scores in all the given subjects:

If the average of total marks > 80, it would be treated as Grade ‘A’
If the average of total marks > 60 and < = 80, it would be treated as Grade ‘B’
If the average of total marks > 30 and < = 60, it would be treated as Grade ‘C’
If the average of total marks > 0 and < = 30, it would be treated as Grade ‘D’

We’ll be following two ways to achieve the same functionality.

  • Without loop
  • With loop

Example 1 . Java Program to Calculate and Print Grades of a Student

Program : 

public class StudentGrades {

    /**
     * A simple Java program to  find student grades
     * Assuming a student has 5 subjects
     **/
    public static void main(String[] args) {
        int totalSubjects = 5;   // assuming 5 subjects
        int maxMarksEachSub = 100;  // Each subject can have max 100 marks 
        double[] marks = new double[totalSubjects];
        double totalMarks = 0;
        double marksInPercentage;
        Scanner userInputObj = new Scanner(System.in);

        System.out.println("Please enter marks in all subjects.");
        for (int i = 0; i < marks.length; i++) {
            System.out.print("Subject " + (i + 1) + " : ");
            marks[i] = userInputObj.nextInt();

            /* Handling Invalid Input   */
            if (marks[i] > maxMarksEachSub || marks[i] < 0) {
                System.out.println("Invalid input " + marks[i] + "\nTerminating the program..... ");
                return;
            }
            totalMarks = totalMarks + marks[i];
        }
        marksInPercentage = (totalMarks / (totalSubjects * maxMarksEachSub)) * 100;

        System.out.println("Total marks obtained : " + totalMarks);
        System.out.println("Your Grade : " + getGrade(marksInPercentage));

    }

    private static String getGrade(double marksInPercentage) {
        if (marksInPercentage > 80 && marksInPercentage <= 100) {
            return "A";
        } else if (marksInPercentage > 60 && marksInPercentage <= 80) {
            return "B";
        } else if (marksInPercentage > 30 && marksInPercentage <= 60) {
            return "C";
        } else {
            return "D";
        }
    }
}

Output :  Student grades

Invalid Input : 

StudentGrades 2

Example 2. Java Program to Calculate and Print Grades of a Student without loop

import java.util.Scanner;

public class StudentGrades {
    public static void main(String[] args) {
        Scanner ed = new Scanner(System.in);
        String name;
        int marks, javaScore, mathScore, webTechScore, cnScore, seScore;
        double avg;
        System.out.print("Enter name of Student = ");
        name = ed.next();
        System.out.println("Now, Number Out of 100 in following subject :- ");
        System.out.print("Java : ");
        javaScore = ed.nextInt();
        System.out.print("Maths : ");
        mathScore = ed.nextInt();
        System.out.print("Web Technologies : ");
        webTechScore = ed.nextInt();
        System.out.print("Computer Network : ");
        cnScore = ed.nextInt();
        System.out.print("Software Engineering : ");
        seScore = ed.nextInt();
        marks = javaScore + mathScore + cnScore + seScore + webTechScore;
        avg = ((double) marks) / 500;
        System.out.println("Name = " + name);
        System.out.println("total marks " + marks);
        if (avg > 0.8 && avg <= 1) {
            System.out.println("Grade = A");
        } else if (avg > 0.6 && avg <= 0.8) {
            System.out.println("Grade = B");
        } else if (avg > 0.3 && avg <= 0.6) {
            System.out.println("Grade = C");
        } else if (avg >= 0 && avg <= 0.3) {
            System.out.println("Grade = D");
        } 
    }
}

Output:

STUDENT GRADES PROGRAM

Example 3 . Java Program to Calculate and Print Grades of a Student using for loop

Program:

import java.util.Scanner;

public class StudentGrades {
    public static void main(String[] args) {
        Scanner ed = new Scanner(System.in);
        String name;
        int marks = 0, subjectCount;
        double avg;
        System.out.print("Enter name of Student = ");
        name = ed.next();
        System.out.print("Enter Number of Subjects = ");
        subjectCount = ed.nextInt();
        System.out.println("Now, Number Out of 100 in following subject :- ");
        for (int i = 0; i < subjectCount; i++) {
            marks += ed.nextInt();
        }
        avg = ((double) marks) / subjectCount;
        System.out.println("Name = " + name);
        System.out.println("total marks " + marks);
        if (avg > 80 && avg <= 100) {
            System.out.println("Grade = A");
        } else if (avg > 60 && avg <= 80) {
            System.out.println("Grade = B");
        } else if (avg > 30 && avg <= 60) {
            System.out.println("Grade = C");
        } else if (avg >= 0 && avg <= 30) {
            System.out.println("Grade = D");
        }
    }
}

Output:

Enter name of Student = ProgrammerBay
Enter Number of Subjects = 5
Now, Number Out of 100 in following subject :- 
83
65
81
67
72
Name = ProgrammerBay
total marks 368
Grade = B

 

Leave a Reply