In this Java program, we will learn how to Calculate Average Using Arrays in your Java program.
How to Calculate Average Using Arrays using JAVA?
Example 1:
RUN CODE SNIPPETpublic class Main { public static void main(String[] args) { double[] numArray = { 54.5, 57.6, -54.9, 100, 99, 45.6 }; double sum = 0.0; for (double num: numArray) { sum += num; } double average = sum / numArray.length; System.out.format("The average is: %.2f", average); } }
OUTPUT
The average is: 50.30