Program to Find Average in Java using Array

Write description:- Write a Java program to calculate the average of numbers or find the average of elements of the given array.

To calculate the average of numbers given in the array, first, we need to calculate the sum and then find the average using sum/number_of_array_elements.

Example:-
array[] = {10, 20, 30, 40, 50};
then sum of array elements = 10+20+30+40+50 = 150
and the average of numbers = 150/5 = 30

Procedure to develop the Java program to find the average,
1) Take numbers as input and store them into an array
2) Declare a sum variable and initialize it with 0
3) Iterate through the array and add numbers to the sum variable and update the sum variable (i.e. sum = sum + array[i])
4) Calculate the average, avg = sum/number_of_array_elements
5) Display the average value

Java program to calculate average of an array

public class ArrayAverage {
  public static void main(String[] args) {

    double array[] = {10, 20, 30, 40, 50};
    // declare sum variable, & initialize with 0
    double sum = 0.0;
    // declare average variable
    double avg = 0.0;

    // loop to iterate the array
    for (int i=0; i<array.length; i++) {
      // add numbers
      sum = sum + array[i];
    }

    // calculate the average value
    avg = sum/array.length;

    // display result
    System.out.println("Average: " + avg );
  }
}

Output:-

Average: 30.0

In the previous Java program, we have hardcoded the values. Now, let us develop another Java program to calculate the average of an array and take numbers as input values. To take input value we can use Scanner class or BufferedReader class, but here we are using the Scanner class. We need to import java.util.Scanner class to use Scanner class methods.

Java program to calculate average of numbers by taking inputs from end-user

import java.util.Scanner;
public class ArrayAverage {
  public static void main(String[] args) {

    // create Scanner class object
    Scanner scan = new Scanner(System.in);

    // take total number of elements
    System.out.print("Enter number of elements: ");
    int size = scan.nextInt();

    // declare an array with given size
    double array[] = new double[size];
    // declare sum variable, & initialize with 0
    double sum = 0.0;
    // declare average variable
    double avg = 0.0;

    // take numbers and store them to the array
    System.out.println("Enter array elements: ");
    for (int i=0; i<array.length; i++) {
      array[i] = scan.nextDouble();
      // add numbers
      sum = sum + array[i];
    }

    // calculate the average value
    avg = sum/array.length;

    // display result
    System.out.println("Average: " + avg );
  }
}

Output for the different test-cases:-

Enter Number of elements: 5
Enter array elements:
10 20 30 40 50
Average: 30.0

Enter Number of elements: 3
Enter array elements:
19.5 20.9 55.95
Average: 32.11666666666667

In this program, we have calculated the sum value while taking the input values. You can take a separate loop and add numbers in that specific loop.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Also See:-

Leave a Reply