Core Java

Java Program to Calculate Average Using Arrays

A quick and practical guide to find and to calculate the average of numbers in array using java language.

1. Overview

In this article, you’ll learn how to calculate the average of numbers using arrays

You should know the basic concepts of a java programming language such as Arrays and forEach loops.

We’ll see the two programs on this. The first one is to iterate the arrays using for each loop and find the average.

In the second approach, you will read array values from the user.

Let us jump into the example programs.

2. Example 1 to calculate the average using arrays

First, create an array with values and run. the for loop to find the sum of all the elements of the array.

Finally, divide the sum with the length of the array to get the average of numbers.

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
package com.javaprogramto.programs.arrays.average;
 
public class ArrayAverage {
 
    public static void main(String[] args) {
 
        // create an array
        int[] array = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 };
 
        // getting array length
        int length = array.length;
 
        // default sium value.
        int sum = 0;
 
        // sum of all values in array using for loop
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
 
        double average = sum / length;
         
        System.out.println("Average of array : "+average);
 
    }
 
}

Output:

1
Average of array : 6.0

3. Example 2 to find the average from user inputted numbers

Next, let us read the input array numbers from the user using the Scanner class.

Scanner Example to add two numbers

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.util.Scanner;
 
public class ArrayAverageUserInput {
 
    public static void main(String[] args) {
 
        // reading the array size.
        Scanner s = new Scanner(System.in);
 
        System.out.println("Enter array size: ");
        int size = s.nextInt();
        // create an array
        int[] array = new int[size];
 
        // reading values from user keyboard
        System.out.println("Enter array values :  ");
        for (int i = 0; i < size; i++) {
            int value = s.nextInt();
            array[i] = value;
 
        }
 
        // getting array length
        int length = array.length;
 
        // default sium value.
        int sum = 0;
 
        // sum of all values in array using for loop
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
 
        double average = sum / length;
 
        System.out.println("Average of array : " + average);
 
    }
 
}

Output:

1
2
3
4
5
6
7
8
9
Enter array size:
5
Enter array values : 
12
23
34
45
56
Average of array : 34.0

4. Conclusion

In this article, you’ve seen how to calculate the average number in an array.

All examples shown are in GitHub.

Average

Published on Java Code Geeks with permission by Venkatesh Nukala, partner at our JCG program. See the original article here: Java Program to Calculate Average Using Arrays

Opinions expressed by Java Code Geeks contributors are their own.

Venkatesh Nukala

Venkatesh Nukala is a Software Engineer working for Online Payments Industry Leading company. In my free time, I would love to spend time with family and write articles on technical blogs. More on JavaProgramTo.com
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

2 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Aaaa
Aaaa
1 year ago

There is a mistake in both programs. If You divide in Java two integers, You will get an integer result. To correct it cast at least one of the operands to double, like this:double average = (double)sum/length;

Bill Fly
Bill Fly
11 months ago

Better method:

double average = arrray.stream()
.mapToDouble(Integer::doubleValue)
.average()
.orElse(0.0);

Back to top button