Calculate An Average Value Of Elements Of An Array | Java Programs

Output:

Average Of Elements Of An Array ***

Array Elements Are: [5, 3, 4, -4, -2, 4]
Average value of the array elements is : 1.6666666666666667

Click Here For Java Online Compiler

Solution:

import java.util.Arrays;

public class ArrayAverage {

    public static void main(String[] args) {

        int[] array = {5, 3, 4, -4, -2, 4};

        double sum = 0;
        for (int i = 0; i < array.length; i++) {
            sum = sum + array[i];
        }

        double average = sum / array.length;
        System.out.println("Average Of Elements Of An Array ***" + "\n");
        System.out.println("Array Elements Are: " + Arrays.toString(array));
        System.out.println("Average value of the array elements is : " + average);
    }
}
Share This :

Related Post



sentiment_satisfied Emoticon