# Java Program to Calculate Average Using Arrays

24
views
(Last Updated On: July 24, 2018)

## In this Programme, you’ll learn Program to Calculate Average Using Arrays in Java.

To nicely understand this example, you should have knowledge of  Java Program

#### Program to Calculate Average Using Arrays

```public class Average {

public static void main(String[] args) {
double[] numArray = { 45.3, 67.5, -45.6, 20.34, 33.0, 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: 27.69`

In the above program, the numArray stores the floating point values whose average is to be found.

Then, to calculate the average, we need to first calculate the sum of all elements in the array. This is done using a for-each loop in Java.

Finally, we calculate the average by the formula:

`average = sum of numbers / total count`

In this case, the total count is given by `numArray.length`.

Finally, we print the average using `format()` function so that we limit the decimal points to only 2 using `"%.2f"`

More Examples