Java – Calculate Average of Numbers

To calculate the average of numbers in an array or arraylist, use a loop statement to find the sum of all the numbers in the array, and divide the sum with number of elements.

In this tutorial, we write Java programs to compute average of numbers in an array and ArrayList.

Example 1 – Average of Numbers in Array

In this example, we shall use Java While Loop, to compute the average.

Algorithm

We shall use the following algorithm to find the average of numbers.

  1. Start.
  2. Read array of numbers. Or initialize an array with number, of whom you would like to find average.
  3. Initialize sum = 0;
  4. Initialize i = 0;
  5. Check if i is less than number of elements in array. If not go to step 8.
  6. Add sum with number in array at index i, and store in the sum itself.
  7. Increment i. Go to step 5.
  8. Compute average = sum / number of elements in array.
  9. Stop.

Java Program

</>
Copy
/**
 * Java Program - Average of Numbers
 */

public class Average {

	public static void main(String[] args) {
		//numbers
		int[] nums = {1, 2, 3, 4, 5, 6};
		
		float sum = 0;
		
		//compute sum
		int i=0;
		while(i < nums.length) {
			sum += nums[i];
			i++;
		}
		
		//compute average
		float average = (sum / nums.length); 
		
		System.out.println("Average : "+average);
	}
}

Note: We are using float datatype for sum variable to save the precision after computing average. If you use int datatype for sum, when dividing it with number of elements, we lose the decimal part of the average.

Output

Average : 3.5

Example 2 – Average of Numbers in Array

In this example, we shall use Java Advanced For Loop, to compute the average.

Algorithm

We shall use the following algorithm to find the average of numbers.

  1. Start.
  2. Read array of numbers. Or initialize an array with numbers, of whom you would like to find average.
  3. Initialize sum = 0;
  4. For each number in the array, add the number to sum.
  5. Compute average = sum / number of elements in array.
  6. Stop.

Java Program

</>
Copy
/**
 * Java Program - Average of Numbers
 */

public class Average {

	public static void main(String[] args) {
		//numbers
		int[] nums = {1, 2, 3, 4, 5, 6};
		float sum = 0;
		
		//compute sum
		for(int num:nums)
			sum += num;
		
		//compute average
		float average = (sum / nums.length); 
		
		System.out.println("Average : "+average);
	}
}

Output

Average : 3.5

Example 3 – Average of Numbers in ArrayList

In this example, we shall use Java Advanced For Loop, to compute the average.

Algorithm

We shall use the following algorithm to find the average of numbers.

  1. Start.
  2. Read array of numbers. Or initialize an ArrayList with numbers, of whom you would like to find average.
  3. Initialize sum = 0;
  4. For each number in the ArrayList, add the number to sum.
  5. Compute average = sum / number of elements in array.
  6. Stop.

Java Program

</>
Copy
import java.util.ArrayList;

/**
 * Java Program - Average of Numbers
 */

public class Average {

	public static void main(String[] args) {
		//numbers
		ArrayList<Integer> nums = new ArrayList<Integer>();
		nums.add(10);
		nums.add(13);
		
		float sum = 0;
		
		//compute sum
		for(int num:nums) {
			sum += num;
		}
		
		//compute average
		float average = (sum / nums.size()); 
		
		System.out.println("Average : "+average);
	}
}

Output

Average : 11.5

Conclusion

In this Java Tutorial, we learned how to find the average of numbers in an array or a ArrayList, using looping statements.