Java Program to Find the Average of Array Elements

In Java, arrays are used to store multiple values in a single variable. A common task with arrays is calculating the average of the numbers stored in the array. The average is the sum of all the numbers divided by how many numbers there are.

In this article, we will learn how to write a simple Java program that calculates the average of the numbers in an array.

Steps to Calculate the Average of Array Elements

  1. Create an array: First, we need to create an array and add some numbers to it.
  2. Add the array elements: To find the average, we first need to add up all the numbers in the array.
  3. Divide by the number of elements: Finally, we calculate the average by dividing the sum by how many numbers are in the array.

Java Code to Find the Average of Array Elements

import java.util.Scanner;

public class AverageOfArray {

    // Method to calculate the average of array elements
    public static double calculateAverage(int[] array) {
        int sum = 0;
        
        // Add up the array elements
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        
        // Calculate the average
        return (double) sum / array.length;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        
        // Get the size of the array from the user
        System.out.print("Enter the number of elements in the array: ");
        int n = sc.nextInt();
        
        // Create the array
        int[] array = new int[n];
        
        // Get the elements of the array from the user
        System.out.println("Enter the elements of the array:");
        for (int i = 0; i < n; i++) {
            array[i] = sc.nextInt();
        }
        
        // Call the method to calculate the average
        double average = calculateAverage(array);
        
        // Print the average
        System.out.println("The average of the array elements is: " + average);
        
        sc.close();
    }
}

How does the Code Work?

  1. Taking Input
    • We use Scanner to ask the user to enter the number of elements in the array and then the values for each element.
  2. Method to Calculate Average
    • The calculateAverage(int[] array) method calculates the sum of all the numbers in the array using a for loop.
    • After adding all the numbers together, we divide the sum by the number of elements in the array to get the average.
    • We use (double) to make sure the average is a decimal number, even if the sum is divisible evenly.
  3. Main Method
    • In the main part of the program, we ask the user to input the number of elements and the values for the array.
    • Then, we call the calculateAverage() method to find the average and display it.

Output

In this example

  • The sum of the elements is 10+20+30+40+50=15010 + 20 + 30 + 40 + 50 = 15010+20+30+40+50=150.
  • The number of elements is 5.
  • So, the average is 1505=30.0\frac{150}{5} = 30.05150โ€‹=30.0.

Conclusion

This Java program shows how to calculate the average of numbers in an array. By adding all the numbers and dividing by the total count, we can easily find the average. This is a simple but important concept in programming, and learning how to do it helps you understand how to work with arrays and numbers in Java.