Java Program to Calculate Average Using Array

In this article, you will learn how to calculate the average using an Array in java. Here we use an integer Array and a for loop for this program so you should have at least a basic understanding of the java array and looping concept.

FORMULA:
Average of an Array = The total sum of all the Array elements / Size of the Array

Algorithm:

  1. Initialize an array arr, a variable sum, and a variable average.
  2. Initialize the value of sum=0, and average=0.0f
  3. Start a for loop from index 0 to the length of the array – 1.
  4. In each iteration, perform sum = sum + arr[i].
  5. After the completion of the loop, Calculate the average= sum/length of the array.
  6. Finally, print the value of the average.

Let’s jump to some examples now,

Example #1. calculate the average using an array

In this example, we just initialize an integer array, after that calculating the total sum of all array elements then divide the sum by the length of the array to get the average. let’s see the java program for it below:

package com.javacodepoint.array;

/*
 * Calculating the average of a given integer Array
 */
public class CalculateAverageUsingArray {

	public static void main(String[] args) {

		// Declaring and Initializing an integer array
		int arr[] = { 2, 5, 1, 12, 8, 5, 7, 10 };

		// Declaring a sum variable and Initializing with 0
		// to calculate the sum of all array's elements.
		int sum = 0;

		// Declaring it for calculating the average
		float average = 0.0f;

		// Iterating all array element to calculate the sum
		for (int i = 0; i < arr.length; i++) {
			sum += arr[i];
		}

		// Calculating the average
		average = (float) sum / arr.length;

		// Printing the calculated average value
		System.out.println("The average of the given array: " + average);

	}

}

OUTPUT:

The average of the given array: 6.25

Example #2. calculate the average using an array for the user-given value

In this example, we will ask the user to decide the size of the array and provides its element value. To take inputs from users, we used java.lang.Scanner class here. Let’s see the complete java program below:

package com.javacodepoint.array;

import java.util.Scanner;

/*
 * Calculating the average of a given integer Array,
 * taking the array elements from user.
 */
public class CalculateAverageUsingArray2 {

	public static void main(String[] args) {

		// Creating scanner object for reading user inputs
		Scanner sc = new Scanner(System.in);
		
		// Read the size of the Array
		System.out.println("Enter the size of the Array: ");
		int n = sc.nextInt();
		
		// Declare the integer Array of given size
		int arr[] = new int[n];
		
		// Declaring a sum variable and Initializing with 0
		// to calculate the sum of all array's elements.
		int sum = 0;

		// Declaring it for calculating the average
		float average = 0.0f;

		// Reading the Array values
		System.out.println("Enter " + n + " element(s) of the Array: ");
		for (int i = 0; i < n; i++) {
			arr[i] = sc.nextInt();
		}

		// Iterating all array element to calculate the sum
		for (int i = 0; i < n; i++) {
			sum += arr[i];
		}

		// Calculating the average
		average = (float) sum / n;

		// Printing the calculated average value
		System.out.println("The average of the given Array: " + average);

	}

}

OUTPUT:

Enter the size of the Array:
5
Enter 5 element(s) of the Array:
4
3
7
8
1
The average of the given Array: 4.6

Example #3. calculate average using for each loop in java

This example program logic is almost similar to Example-1 but the only difference here is we used a for-each loop to iterate the array elements. Let’s see the java program below:

package com.javacodepoint.array;

/*
 * Calculating the average of an Array using for each loop
 */
public class CalculateAverageUsingArray3 {

	public static void main(String[] args) {

		// Declaring and Initializing an integer array
		int arr[] = { 4, 5, 1, 6, 8 };

		// Declaring a sum variable and Initializing with 0
		// to calculate the sum of all array's elements.
		int sum = 0;

		// Declaring it for calculating the average
		float average = 0.0f;

		// Iterating all array element using for-each loop
		for (int value : arr) {
			sum += value;
		}

		// Calculating the average
		average = (float) sum / arr.length;

		// Printing the calculated average value
		System.out.println("The average of the given array: " + average);

	}

}

OUTPUT:

The average of the given array: 4.8

Java logical programs list


Java Basic Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *