Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade
Java program to enter marks of five subjects and calculate total, percentage, and grade.
In this article, you will learn how to write a java program to enter marks of five subjects and calculate the Total, Percentage, and Grade.
Example
Enter the marks of five subjects::
89
67
76
90
70
The Total marks = 392.0/500.0
The Average marks = 78.4
The Percentage = 78.4%
The Grade = 'C'
You should have the knowledge of following these topics in java programming to understand this example:
- Java
java.util.Scanner
package - Java
main()
method - Java
System.out.println()
method - Java
If-else
statement - Java
Switch
statement - Java Functions
Standard Formula
Total = marks1 + marks2 + marks3 + marks4 + marks5
Average = Total / 5.0
Percentage = (Total / 500.0) x 100
Where marks1
, marks2
, marks3
, marks4
, and marks5
are the marks of five subjects.
In this article, we solve this problem in three methods:
Source Code
// Java Program to Enter Marks of Five Subjects and Calculate Total, Percentage, and Grade
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the marks of five subjects::\n");
// To store the values of five subjects
float sub_1 = in.nextFloat();
float sub_2 = in.nextFloat();
float sub_3 = in.nextFloat();
float sub_4 = in.nextFloat();
float sub_5 = in.nextFloat();
float total;
float average;
float percentage;
char grade;
// Calculate total, average and percentage
total = sub_1 + sub_2 + sub_3 + sub_4 + sub_5;
average = (float)(total / 5.0);
percentage = (float)((total / 500.0) * 100);
// It will calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// It will produce the final output
System.out.println("\nThe Total marks = " + total + "/500.0");
System.out.println("The Average marks = " + average);
System.out.println("The Percentage = " + percentage + "%");
System.out.println("The Grade = '" + grade + "'");
}
}
Output
Enter the marks of five subjects::
89
67
76
90
70
The Total marks = 392.0/500.0
The Average marks = 78.4
The Percentage = 78.4%
The Grade = 'C'
Explanation
Average: we take the marks of five subjects as input after that the sum of these marks is divided by 5
then It will return the average value of the marks.
Percentage: we made a division of the sum of user's marks by 500
then It multiplied by 100
then It will return the percentage value of the marks.
Grade: we compared the value of the Average marks with this range of marks to find the Grade of students like:
If Average marks >= 90
then Its Grade is 'A'
If Average marks >= 80 and < 90
then Its Grade is 'B'
If Average marks >= 70 and < 80
then Its Grade is 'C'
If Average marks >= 60 and < 70
then Its Grade is 'D'
If Average marks < 60
then Its Grade is 'E'
Java Program to Calculate Percentage of Marks using Switch Case
// Java Program to Calculate Percentage of Marks using Switch Case
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the marks of five subjects::\n");
// To store the values of five subjects
float sub_1 = in.nextFloat();
float sub_2 = in.nextFloat();
float sub_3 = in.nextFloat();
float sub_4 = in.nextFloat();
float sub_5 = in.nextFloat();
float total;
float average;
float percentage;
char grade;
int sAvg;
// Calculate total, average and percentage
total = sub_1 + sub_2 + sub_3 + sub_4 + sub_5;
average = (float)(total / 5.0);
percentage = (float)((total / 500.0) * 100);
// It will divides average by 10 check switch expression
sAvg = (int) average / 10;
// It will calculate the Grade
switch (sAvg) {
case 10:
grade = 'A';
break;
case 9:
grade = 'A';
break;
case 8:
grade = 'B';
break;
case 7:
grade = 'C';
break;
case 6:
grade = 'D';
break;
default:
grade = 'E';
break;
}
// It will produce the final output
System.out.println("\nThe Total marks = " + total + "/500.0");
System.out.println("The Average marks = " + average);
System.out.println("The Percentage = " + percentage + "%");
System.out.println("The Grade = '" + grade + "'");
}
}
You should also read these things
C Program to Calculate Percentage and Grade
C++ Program to Calculate Sum of 5 Subjects and Find Percentage and Grade
Python Program to Calculate Total Marks Percentage and Grade of a Student
Java Program to Calculate Percentage of Marks using Function
// Java Program to Calculate Percentage of Marks using Function
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
System.out.println("Enter the marks of five subjects::\n");
// To store the values of five subjects
float sub_1 = in.nextFloat();
float sub_2 = in.nextFloat();
float sub_3 = in.nextFloat();
float sub_4 = in.nextFloat();
float sub_5 = in.nextFloat();
// This will display the above calculation
CalcPercentGrade(sub_1, sub_2, sub_3, sub_4, sub_5);
}
// This function will display the percentage, average, and grade
private static void CalcPercentGrade(float sub_1, float sub_2, float sub_3, float sub_4, float sub_5) {
float total;
float average;
float percentage;
char grade;
// Calculate total, average and percentage
total = sub_1 + sub_2 + sub_3 + sub_4 + sub_5;
average = (float)(total / 5.0);
percentage = (float)((total / 500.0) * 100);
// It will calculate the Grade
if (average >= 90)
grade = 'A';
else if (average >= 80 && average < 90)
grade = 'B';
else if (average >= 70 && average < 80)
grade = 'C';
else if (average >= 60 && average < 70)
grade = 'D';
else
grade = 'E';
// It will produce the final output
System.out.println("\nThe Total marks = " + total + "/500.0");
System.out.println("The Average marks = " + average);
System.out.println("The Percentage = " + percentage + "%");
System.out.println("The Grade = '" + grade + "'");
}
}
Output
Enter the marks of five subjects::
98
87
95
70
80
The Total marks = 430.0/500.0
The Average marks = 86.0
The Percentage = 86.0%
The Grade = 'B'