Open In App

Java Program to illustrate Total Marks and Percentage Calculation

Improve
Improve
Like Article
Like
Save
Share
Report

Java Program to illustrate Total marks and Percentage Calculation can be done using storing marks of all subjects in an array and taking the summation of marks of all subjects and taking the average of all marks respectively.

Example

Input: N = 5, marks[] = {70, 60, 90, 40, 80}
Output: Total Marks = 340
        Total Percentage = 68%
        
Input: N = 5, marks[] = {60, 70, 80, 90, 100}
Output: Total Marks = 400
        Total Percentage = 80%

Approach : 

  • Enter the number of subjects N and marks of those subjects into a 1-D array, say, marks[].
  • Create a variable total_marks which stores the total of all the marks.
  • To print the percentage of that student, divide total_marks with N.

Below are the examples of the above approach.

Java




// Java Program to illustrate Total
// marks and Percentage Calculation
import java.io.*;
class GFG {
    public static void main(String[] args)
    {
        int N = 5, total_marks = 0;
        float percentage;
  
        // create 1-D array to store marks
        int marks[] = { 89, 75, 82, 60, 95 };
  
        // calculate total marks
        for (int i = 0; i < N; i++) {
            total_marks += marks[i];
        }
        System.out.println("Total Marks : " + total_marks);
  
        // calculate percentage
        percentage = (total_marks / (float)N);
        System.out.println(
            "Total Percentage : " + percentage + "%");
    }
}


Output

Total Marks : 401
Total Percentage : 80.2%

Time Complexity: O(N)

Space Complexity: O(1)


Last Updated : 09 Nov, 2020
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads