Total Marks and Percentage Calculation in Java



We will demonstrate how total marks and percentages are calculated using Java programs. The term total marks refer to the summation of all the available marks, while the term percentage refers to the number obtained by dividing the calculated marks by total marks and multiplying the resultant by 100.

percentage_of_marks = (obtained_marks/total_marks) × 100

Problem Statement

Given a Java program to calculate the total marks and percentage of a student in a given set of subject.

Input

marks = { 69, 88, 77, 89, 98, 100, 57, 78 }

Output

Total Marks is: 656
Total Percentage is: 82.0%

Approach to Illustrate Total Marks and Percentage Calculation (Without User Input)

Below are the steps which will give us the idea to calculate total marks and percentage calculation (without user input) ?

  • Step 1: Define the number of subjects.
  • Step 2: Create an array to store the marks of each subjects.
  • Step 3: Sum up all the marks to calculate the total marks.
  • Step 4: Divide the total marks by number of subjects and multiply by 100 to calculate percentage.
  • Step 5: Print and display the total marks and percentage.

Example 1

This is a Java program to demonstrate how total marks and percentages are calculated.

// Java Program to demonstrate how is Total marks and Percentages calculated
import java.io.*;
public class TotalMarks_Percent1 {
	public static void main(String[] args){
		int n = 8, t_marks = 0;
		float percent;
		// creation of 1-D array to store marks
		int marks[] = { 69, 88, 77, 89, 98, 100, 57, 78 };
		// calculation of total marks
		for (int j = 0; j < n; j++) {
			t_marks += marks[j];
		}
		System.out.println("Total Marks is: " + t_marks);
		// calculate the percentage
		percent = (t_marks / (float)n);
		System.out.println("Total Percentage is: " + percent + "%");
	}
}

Output

Total Marks is: 656
Total Percentage is: 82.0%

In the above Java program, the total marks obtained by a student in 8 subjects and their percentage is being calculated. The obtained marks are stored in an array named marks [].

The total marks are calculated and stored in a variable named t_marks and their percentages are calculated and stored in a variable named percent.

Both values are further displayed on the console.

Approach to Illustrate Total Marks and Percentage Calculation (With User Input)

Below are the steps which will give us the idea to calculate total marks and percentage calculation (with user input)

  • Step 1: Take the input marks from the user.
  • Step 2: Calculate the total marks by summing up all the marks.
  • Step 3: Calculate the total percentage by dividing total marks by maximum possible marks (here it is 500)
  • Step 4: Divide the total marks by number of subjects and multiply by 100 to calculate percentage.
  • Step 5: Print and display the total marks and percentage.

Example 2

This is a Java program to demonstrate the calculation of the total marks and percentage of five subjects taken as input from the user.

// Java program to compute the total marks and percentage of five subjects taken as input from the user
import java.util.Scanner;
class TotalMarks_Percent2{
public static void main(String args[]){
    float FLAT, COA, Networking, Python, AI; 
    double t_marks, percent;
    Scanner mk =new Scanner(System.in);
    // Take marks as input of 5 subjects from the user 
    System.out.println("Input the marks of five subjects \n");
    System.out.print("Enter marks of FLAT:");
    FLAT = mk.nextFloat();
    System.out.print("Enter marks of COA:");
    COA = mk.nextFloat();
    System.out.print("Enter marks of Networking:");
    Networking = mk.nextFloat();
    System.out.print("Enter marks of Python:");
    Python = mk.nextFloat();
    System.out.print("Enter marks of AI:");
    AI = mk.nextFloat();
    // Calculation of total marks and percentage obtained in 5 subjects
    t_marks = FLAT + COA + Networking + Python + AI;
    percent = (t_marks / 500.0) * 100;
    // display the results 
    System.out.println("Total marks obtained in 5 different subjects ="+t_marks);    
    System.out.println("Percentage obtained in these 5 subjects = "+percent);
   }
}

Output

Input the marks of five subjects 

Enter marks of FLAT:98
Enter marks of COA:56
Enter marks of Networking:67
Enter marks of Python:89
Enter marks of AI:78
Total marks obtained in 5 different subjects =388.0
Percentage obtained in these 5 subjects = 77.60000000000001

In the above Java program, the marks obtained in 5 different subjects, namely FLAT, COA, Networking, Python, and AI are taken as input from the user.

The marks are taken as input from the user and are stored in variables of float data type. Further, the total marks obtained in these subjects are calculated by the addition of the marks of each subject and are stored in a variable named t_marks.

Finally, the program displays the total marks obtained in the stated subjects and their percentage.

This article threw light on two ways to calculate total marks and their percentage. The article began with a discussion of term percentage and total marks. The first method discusses the method without taking input from a user and in the second method, the value of marks is taken as input from a user and their sum and percent are calculated and displayed.

Updated on: 2024-07-09T16:03:18+05:30

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements