Java program to find Total Average and Percentage of Five Subjects

Write a Java program to find Total Average and Percentage of Five Subjects or N number of subjects with example.

Java program to find Total Average and Percentage of Five Subjects Example 1

This Java program allows users to enter five different values for five subjects. And then, this Java program finds the Total, Average, and Percentage of those Five Subjects. In this example, we are using the Arithmetic Operators to perform arithmetic operations.

// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;

public class Totalof5subjects1 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int english, chemistry, computers, physics, maths; 
	    float total, Percentage, Average;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Five Subjects Marks : ");
		english = sc.nextInt();	
		chemistry = sc.nextInt();	
		computers = sc.nextInt();	
		physics = sc.nextInt();	
		maths = sc.nextInt();	
		
		total = english + chemistry + computers + physics + maths;
		Average = total / 5;
	    Percentage = (total / 500) * 100;
	    
	    System.out.println(" Total Marks =  " + total);
	    System.out.println(" Average Marks =  " + Average);
	    System.out.println(" Marks Percentage =  " + Percentage);
	}
}
Java program to find Total Average and percentage of Five Subjects 1

Java program to Calculate Total Average and Percentage of Five Subjects Example 2

The above Java program is for a fixed number of subjects. In this Java program, the user has the flexibility to choose several subjects. And then, it finds the Total marks, its Average, and percentage

// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;

public class Totalof5subjects2 {
	private static Scanner sc;
	public static void main(String[] args) 
	{
		int totalSubjects, i;
	    float Marks, total = 0, Percentage, Average;
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Total Number of Subjects : ");
		totalSubjects = sc.nextInt();
		
		System.out.print(" Please Enter the Subjects Marks : ");
		for(i = 0; i < totalSubjects; i++)
		{
			Marks = sc.nextInt();
			total = total + Marks;
		}
	
		Average = total / totalSubjects;
	    Percentage = (total / (totalSubjects * 100)) * 100;
	    
	    System.out.println(" Total Marks =  " + total);
	    System.out.println(" Average Marks =  " + Average);
	    System.out.println(" Marks Percentage =  " + Percentage);
	}
}

ANALYSIS

  1. The first Java system.out.print statement asks the user to enter the Total number of Subjects. For example, 3 subjects
  2. The For loop resists the user, not to enter more than 3 values by using the condition i < totalsubjects. In the next line, we are adding the user entered value to Total.
  3. Outside the loop, we calculated the average and percentage.
 Please Enter the Total Number of Subjects : 6
 Please Enter the Subjects Marks : 80 85 75 65 90 95
 Total Marks =  490.0
 Average Marks =  81.666664
 Marks Percentage =  81.666664

Java program to Calculate Total Average and Percentage of Five Subjects Example 3

This program is the same as the first example. But in this Java program, we are creating a separate method to find the total, average, and percentage of student marks.

// Java program to find Total Average and percentage of Five Subjects
import java.util.Scanner;

public class Totalof5subjects3 {

	private static Scanner sc;
	public static void main(String[] args) 
	{
		int english, chemistry, computers, physics, maths; 	    
		sc = new Scanner(System.in);
		
		System.out.print(" Please Enter the Five Subjects Marks : ");
		english = sc.nextInt();	
		chemistry = sc.nextInt();	
		computers = sc.nextInt();	
		physics = sc.nextInt();	
		maths = sc.nextInt();	
		
		totalmarks(english, chemistry, computers, physics, maths);
	}	
	public static void totalmarks(int eng, int chem, int com, int phy, int math)
	{
		float total, Percentage, Average;
		total = eng + chem + com + phy + math;
		Average = total / 5;
	    Percentage = (total / 500) * 100;
	    
	    System.out.println(" Total Marks =  " + total);
	    System.out.println(" Average Marks =  " + Average);
	    System.out.println(" Marks Percentage =  " + Percentage);
	}
}
 Please Enter the Five Subjects Marks : 65 75 85 95 90
 Total Marks =  410.0
 Average Marks =  82.0
 Marks Percentage =  82.0