Java Program to Calculate Average Using Arrays

In this program, you’ll learn to calculate the average of the given arrays in Java.

Example: Program to Calculate Average Using Arrays

  1. public class Average {
  2. public static void main(String[] args) {
  3. double[] numArray = { 45.3, 67.5, -45.6, 20.34, 33.0, 45.6 };
  4. double sum = 0.0;
  5. for (double num: numArray) {
  6. sum += num;
  7. }
  8. double average = sum / numArray.length;
  9. System.out.format("The average is: %.2f", average);
  10. }
  11. }

When you run the program, the output will be:

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"