Blog

Articles to grow your career

Article

Arithmetic mean (average) in Java – Algorithms for interview

Consider the implementation of the algorithm for finding the arithmetic mean of the array elements.

First, in a loop, we will iterate over all the elements of the array and calculate their sum. Then we divide the sum by the length of the array:

public class ArithmeticAverage {
    public static void main(String[] args) {
        double[] nums = {10.1, 11.2, 12.3, 13.4, 14.5};
        double result = 0;

        for (double d : nums) {
            result += d;
        }
        System.out.println("Arithmetic Average " + result / nums.length);
    }
}
Alex Kara
By Alex Kara on Apr 01, 2022
Coding tasks for interviews