Java program to find Percentage and Grade based on marks of 5 subjects

In Java, to find percentage and grade of the students based on marks we can use if-else conditional statements or Ternary Operator.

For example, if a student’s marks in each subject are 30, 45, 60, 90, and 67, the calculated percentage would be 58.40%, resulting in a grade of E.

In this article, we are focusing on learning through examples, detailed logic and program explanation for better understanding.

Required Knowledge

Problem Statement

We have to write a java program to calculate percentage and grade of student based on their marks in five individual subjects like Maths, Physics, Chemistry, English and Hindi.

The program should take marks of these five subjects and calculate the percentage and provide grade according to the percentage. This action will be done in our program. Let’s see an example,

Example 1:

For Five subject marks (56,46,78,98 and 78), Output should be 71.2 and grade is C

Explanation:

Maths: 56

Physics: 46

Chemistry: 78

English: 98

Hindi: 78

Percentage: (Total Marks / (maximum marks for subject)*total subjects))*100

Maximum marks per subject assumed as 100.

Percentage: (56+46+78+98+79)/500 = 71.2

Grade is C

Hence, the percentage is 71.2 Student Secured Grade C

Example 2:

For input marks (45.6,75.8,66.8,55.9,34.9), output should be 55.8 and grade is E.

Explanation:

Maths: 45.6

Physics: 75.8

Chemistry: 66.8

English: 55.9

Hindi: 34.9

Percentage: (Total Marks / (maximum marks for subject)*total subjects))*100

Maximum marks per subject assumed as 100.

Percentage: (45.6+75.6+66.8+55.9+34.9)/500 = 55.8

Grade is E

Hence, the percentage is 55.8 and Student Secured Grade E

Logic to Calculate percentage and Grade based on marks of 5 subjects

  • For all the subject marks, we will calculate percentage.
    • percentage = (total marks/ (no. of subjects)* (maximum marks of each subject)).
    • percentage =(mathMarks+physicsMarks+chemistryMarks+englishMarks+hindiMarks)/(5)*(100))
  • After getting the percentage. We will find the grade of students:
    • (percentage>=90 && percentage<=100), Grade A
    • Else, (percentage >= 80 && percentage < 90), then it will be Grade B.
    • Otherwise, if (percentage >= 70 && percentage < 80), It will be Grade C.
    • If not, (percentage >= 60 && percentage < 70), Grade will be E
    • Otherwise, (percentage >= 50 && percentage < 60), Grade is F
    • If none of the conditions are true then the result will be “Student failed”.

Java program to Calculate percentage and Grade based on marks of 5 subjects

Program 1: Using if else

In the below program we are using if else to calculate percentage and grade of students based on marks of 5 subjects in Java.

import java.util.Scanner;

public class GradeBasedOnMarks {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        // Input marks
        System.out.println("Please enter marks of Maths");
        float mathMarks = s.nextFloat();
        System.out.println("Please enter marks of Physics");
        float physicsMarks = s.nextFloat();
        System.out.println("Please enter marks of Chemistry");
        float chemMarks = s.nextFloat();
        System.out.println("Please enter marks of English");
        float englishMarks = s.nextFloat();
        System.out.println("Please enter marks of Hindi");
        float hindiMarks = s.nextFloat();

        // Calculate total marks
        float totalMarks = mathMarks + physicsMarks + chemMarks + englishMarks + hindiMarks;

        // Calculate percentage
        float maxMarksPerSubject = 100;
        float totalSubjects = 5;
        float percentage = (totalMarks / (maxMarksPerSubject * totalSubjects)) * 100;

        // Round percentage to two decimal points
        String formattedPercentage = String.format("%.2f", percentage);

        // Determine grade
        if (percentage >= 90 && percentage <= 100)
            System.out.println(formattedPercentage + " - Student secured Grade A");
        else if (percentage >= 80 && percentage < 90)
            System.out.println(formattedPercentage + " - Student secured Grade B");
        else if (percentage >= 70 && percentage < 80)
            System.out.println(formattedPercentage + " - Student secured Grade C");
        else if (percentage >= 60 && percentage < 70)
            System.out.println(formattedPercentage + " - Student secured Grade D");
        else if (percentage >= 40 && percentage < 60)
            System.out.println(formattedPercentage + " - Student secured Grade E");
        else
            System.out.println(formattedPercentage + " - Student got Grade F");

        // Close scanner
        s.close();
    }
}

Output:

Please enter marks of Maths
65
Please enter marks of Physics
45
Please enter marks of Chemistry
78
Please enter marks of English
86
Please enter marks of Hindi
58
66.40 - Student Secured Grade D

Java program explanation to Calculate percentage and Grade based on marks of 5 subjects

  • The program uses the Scanner class to read user input for marks in five subjects: Maths, Physics, Chemistry, English, and Hindi.
  • It sums the marks obtained in these subjects to calculate the total marks.
  • It calculates the percentage by dividing the total marks by the maximum possible marks (500, assuming each subject has a maximum of 100 marks) and then multiplying by 100.
  • The percentage is rounded to two decimal points using String.format("%.2f", percentage).
  • Based on the calculated percentage, the program determines and prints the corresponding grade (A, B, C, D, E, or F) using a series of if-else statements.
  • Finally, the program closes the Scanner to free up resources.

Program 2: Using Ternary Operator

In the below program we are using Ternary Operator to Calculate percentage and Grade of students based on marks of 5 subjects in Java.

import java.util.Scanner;

public class GradeBasedOnMarks {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        
        // Input marks
        System.out.println("Please enter marks of Maths");
        float mathMarks = s.nextFloat();
        System.out.println("Please enter marks of Physics");
        float physicsMarks = s.nextFloat();
        System.out.println("Please enter marks of Chemistry");
        float chemMarks = s.nextFloat();
        System.out.println("Please enter marks of English");
        float englishMarks = s.nextFloat();
        System.out.println("Please enter marks of Hindi");
        float hindiMarks = s.nextFloat();
        
        // Calculate total marks
        float totalMarks = mathMarks + physicsMarks + chemMarks + englishMarks + hindiMarks;
        
        // Calculate percentage
        float percentage = (totalMarks / 500) * 100;
        String formattedPercentage = String.format("%.2f", percentage);
        
        // Determine grade using ternary operator
        String result = (percentage >= 90 && percentage <= 100) ? formattedPercentage + " - Student secured Grade A"
                : (percentage >= 80 && percentage < 90) ? formattedPercentage + " - Student secured Grade B"
                : (percentage >= 70 && percentage < 80) ? formattedPercentage + " - Student secured Grade C"
                : (percentage >= 60 && percentage < 70) ? formattedPercentage + " - Student secured Grade D"
                : (percentage >= 40 && percentage < 60) ? formattedPercentage + " - Student secured Grade E"
                : formattedPercentage + " - Student failed";
        
        // Print result
        System.out.println(result);
        
        // Close scanner
        s.close();
    }
}

Output:

Please enter marks of Maths
68.9
Please enter marks of Physics
95
Please enter marks of Chemistry
94.9
Please enter marks of English
88.9
Please enter marks of Hindi
34.8
76.50 - Student Secured Grade C

Java program explanation to Calculate percentage and based on marks of 5 subjects

  • The program uses the Scanner class to read user input for marks in five subjects: Maths, Physics, Chemistry, English, and Hindi.
  • It sums the marks obtained in these subjects to calculate the total marks.
  • It calculates the percentage by dividing the total marks by 500 (the maximum possible marks) and then multiplying by 100.
  • The percentage is rounded to two decimal places using String.format("%.2f", percentage).
  • Using a ternary operator, the program determines and prints the corresponding grade (A, B, C, D, E, or failed) based on the calculated percentage.
  • Finally, the program closes the Scanner to free up resources.

Conclusion

In this article, we have learnt how to calculate percentage and grade based on marks of 5 subjects in Java. We have seen the logic and then moved on to writing the program.

For more information on related topics, you might find these articles helpful:

We hope you find this article helpful. If you have any questions or need any further clarification, feel free to leave a comment below. Don’t forget to subscribe to our newsletter to get more updates on Java Programming tips and tutorials.

Happy Java Programming!

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.