Signup/Sign In

Java Program To Find the Sum and Average of an Array

In this tutorial, we will learn how to calculate the sum and average of all the elements of an array. But before moving forward, if you are not familiar with the concepts of the array, then do check the article Arrays in Java.

Input: Enter the Array Elements: 1 2 3 4 5 6 7 8 9 10

Output: Sum= 55

Average= 5.5

Program 1: Calculate the Sum and Average of all the Elements of an Array

In this approach, we will use the iterative method to calculate the sum and average of all the elements in an array.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Call a method that will calculate the sum and average of all the elements 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 average and return it.
  9. Print the average.
  10. Stop.

Below is the code for the same.

The below program demonstrates how to calculate the sum and average of an array using the iterative method.

//Java program to calculate the average of array elements
import java.util.Scanner;
public class Main 
{
    // Function that returns the average of an array.
    static double averageCalculate(int a[], int n)
    {
        // Find sum of array element
        int sum = 0;
        for (int i = 0; i < n; i++)
        {
            sum += a[i];
        }
        System.out.println("The total sum of all the elements in the array is "+sum);
        return (double)sum / n;
    }
     
    //driver code
    public static void main (String[] args)
    {
        Scanner sc=new Scanner(System.in);
        
        int n;  //Declare array size
        System.out.println("Enter the total number of elements in the array ");
        n=sc.nextInt();  //Initialize the array size
        
        int arr[] = new int[n];    //Declare array
        System.out.println("Enter the array elements ");
        for(int i=0;i<n;i++)      //Initialize the array
        {
            arr[i]=sc.nextInt();
        }
     
        System.out.println("The average of all the elements in an array is "+averageCalculate(arr, n));
    }
}
 


Enter the total number of elements in the array 10
Enter the array elements 1 2 3 4 5 6 7 8 9 10
The total sum of all the elements in the array is 55
The average of all the elements in an array is 5.5

Program 2: Calculate the Sum and Average of all the Elements of an Array

In this approach, we will use the recursive method to calculate the sum and average of all the elements in an array.

Algorithm

  1. Start
  2. Declare an array.
  3. Initialize the array.
  4. Call a method that will calculate the sum and average of all the elements in an array.
  5. Use a recursive function to calculate the sum and average of all the elements in an array.
  6. Return the average and sum of all the elements in an array.
  7. Print the average and sum of all the elements in an array.
  8. Stop

Below is the code for the same.

The below program demonstrates how to calculate the sum and average of an array using the recursive function.

//Java program to calculate the average of array elements using recursive function
import java.util.Scanner;

public class Main 
{
     
     // Recursively computes average of a[]
    static double avgCalcRec(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] + avgCalcRec(a, i+1, n))/n);
      
        // Compute sum
        return (a[i] + avgCalcRec(a, i+1, n));
    }
      
    // Function that returns the average of an array.
    static double averageCalculate(int a[], int n)
    {
         return avgCalcRec(a, 0, n);
    }
     
     
    //driver code
    public static void main (String[] args)
    {
        Scanner sc=new Scanner(System.in);
        
        int n;  //Declare array size
        System.out.println("Enter the total number of elements in the array ");
        n=sc.nextInt();  //Initialize the array size
        
        int arr[] = new int[n];    //Declare array
        System.out.println("Enter the array elements ");
        for(int i=0;i<n;i++)      //Initialize the array
        {
            arr[i]=sc.nextInt();
        }
        //Print the average
        double avg=averageCalculate(arr, n);     
        System.out.println("The average of all the elements in an array is "+avg);

        //Print the sum
        double sum=avg*n;
        System.out.println("The sum of all the elements in an array is "+sum);
    }
}
 


Enter the total number of elements in the array 10
Enter the array elements 32 1 4 42 56 78 96 23 13 31
The average of all the elements in an array is 37.6
The sum of all the elements in an array is 376.0



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.