Signup/Sign In

Java Program To Accept the Marks of a Student and Find the Total Marks and Percentage

In this tutorial, we will learn how to accept the marks of a student into a 1-D array and find the total marks and percentage. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input:

Marks = 40 50 80 90 60 70

Output:

Total Marks = 390

Total percentage = 65%

Program 1: Find the Total Marks and Percentage of a Student

In this approach, we will use the iterative method to calculate the total marks and percentage secured by the student.

Algorithm

  1. Start
  2. Declare an array.
  3. Ask the user to initialize the array.
  4. Call a method that will calculate the sum and percentage of all the elements(marks) in an array.
  5. Declare a sum variable there and initialize it to 0.
  6. Update the sum in each iteration.
  7. Print the sum.
  8. Calculate the percentage.
  9. Print the percentage secured.
  10. Stop.

Below is the code for the same.

The below program demonstrates how to accept the marks of a student into an array and find the total marks and percentage using the iterative method.

/* Java Program to accept the marks of a student into a 1-D array and find the total marks and percentage. */
import java.util.*;   
public class Main 
{ 
    // Main driver method 
    public static void main(String args[]) 
    {   
        // Taking input from user 
        Scanner sc = new Scanner(System.in); 
        int n;     //Declaring Variables
        
        //Ask the user to enter the Array Size
        System.out.println("Enter the total subjects ");
        n=sc.nextInt();
        
        //Declare the array
        int arr[] = new int[n]; 
        System.out.println("Enter the marks secured in each subject ");
        for(int i=0;i<n;i++)   //Initialize the array
        {
            arr[i]=sc.nextInt();
        }
        
        int total=0;
        //Calculate the total marks
        for(int i=0;i<n;i++)
        {
            total=total+arr[i];
        }
        //Display the total marks
        System.out.println("The total marks obtained is "+total);
        
        //Calculate the percentage
        float percentage; 
        percentage = (total / (float)n); 
        
        //Display the total percentage
        System.out.println( "Total Percentage : " + percentage + "%");                         
    }   
}


Enter the total subjects 6
Enter the marks secured in each subject 78 98 67 90 34 23
The total marks obtained is 390
Total Percentage : 65.0%

Program 2: Find the Total Marks and Percentage of a Student

In this approach, we will use the recursive method to calculate the total marks and total percentage secured by the student.

Algorithm

  1. Start
  2. Declare an array.
  3. Ask the user to initialize the array.
  4. Call a method that will calculate the total marks and total percentage secured by the student.
  5. Use a recursive function to calculate the sum and percentage of all the elements in an array.
  6. Return the percentage and sum of all the elements in an array.
  7. Print the percentage and sum of all the elements in an array.
  8. Stop

The below program demonstrates how to accept the marks of a student into an array and find the total marks and percentage using the Recursive method.

/* Java Program to accept the marks of a student into a 1-D array and find the total marks and percentage. */
import java.util.*; 
public class Main 
{ 
    // Recursively computes average of a[]
    static double findTotal(int a[], int i, int n)
    {
        // Last element
        if (i == n-1)
            return a[i];    
        // When index is 0, divide sum computed so
        // far by n.
        if (i == 0)
            return ((a[i] + findTotal(a, i+1, n))/n);    
        // Compute sum
        return (a[i] + findTotal(a, i+1, n));
    }    
    // Function that returns the average of an array.
    static double findPercentage(int a[], int n)
    {
         return findTotal(a, 0, n);
    }    
    // Main driver method 
    public static void main(String args[]) 
    {   
        // Taking input from user 
        Scanner sc = new Scanner(System.in); 
        int n;     //Declaring Variables
        
        //Ask the user to enter the Array Size
        System.out.println("Enter the total subjects ");
        n=sc.nextInt();
        
        //Declare the array
        int arr[] = new int[n]; 
        System.out.println("Enter the marks secured in each subject ");
        for(int i=0;i<n;i++)   //Initialize the array
        {
            arr[i]=sc.nextInt();
        }        
        //Print the sum and percentage
         double avg=findPercentage(arr, n);  
         double sum=avg*n;
        System.out.println("The total marks is "+sum);
        System.out.println("The total percentage is "+avg+" % ");     
    }   
}


Enter the total subjects 6
Enter the marks secured in each subject 87 56 34 24 45 99
The total marks is 345.0
The total percentage is 57.5 %



About the author:
I am the founder of Studytonight. I like writing content about C/C++, DBMS, Java, Docker, general How-tos, Linux, PHP, Java, Go lang, Cloud, and Web development. I have 10 years of diverse experience in software development.