Java Program to Find the Average of an Array

In the previous article, we have seen Java Program to Find the Product of All the Elements of an Array

In this article we are going to see how we can find the average of elements in an array using Java programming language.

Java Program to Find the Average of an Array

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find the average of elements of the array.

Method-1: Java Program to Find the Average of an Array By Static Initialization of Array Elements

Approach:

  • Take an array with elements in it.
  • Print the array elements.
  • Find the sum of all elements by iterating using a for loop.
  • Find average by dividing the sum by the array length.
  • Print average.

Program:

import java.util.Arrays;
import java.util.Scanner;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {12, 2, 34, 20, 54, 6};

        int sum = 0,avg;        
        
        // Adds the sum of all elements
        for(int i=0;i<arr.length;i++)
        {
            sum+=arr[i];
        }

        avg=sum/arr.length;

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the average
        System.out.println("The average of the array is: "+avg);
    }
}
Output:

The array elements are[12, 2, 34, 20, 54, 6]
The average of the array is: 21

Method-2: Java Program to Find the Average of an Array By Dynamic Initialization of Array Elements

Approach:

  • Ask the user to enter the array size and store it.
  • Create an empty array of the specified size.
  • Ask the user to enter the elements.
  • Use a for loop to store the elements. Find the sum of all elements by iterating using a for loop.
  • Find average by dividing the sum by the array length.
  • Print the array elements.
  • Print the average.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        // Taking size as input from the user
        System.out.println("Enter the array size");
        int size = scan.nextInt();

        // Creating the array
        int arr[] = new int[size];

        // Entering the array elements
        System.out.println("Enter array elements");
        int sum = 0,avg;        
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
            // Adds the sum of all elements
            sum+=arr[i];
        }

        avg=sum/arr.length;

        // Prints the array elements
        System.out.println("The array elements are"+Arrays.toString(arr));
        
        // Prints the average
        System.out.println("The average of the array is: "+avg);
    }
}

Output:

Enter the array size 5
Enter array elements 10 20 30 40 50
The array elements are[10, 20, 30, 40, 50]
The average of the array is: 30

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: