Java Program to Find Average of Array
To find the average of an array in Java, sum all elements using a loop and then divide by the number of elements with
average = (double) sum / array.length.Examples
Input[10, 20, 30]
Output20.0
Input[5, 15, 25, 35]
Output20.0
Input[100]
Output100.0
How to Think About It
To find the average, first add all numbers in the array to get the total sum. Then, divide this sum by how many numbers are in the array. This gives the average value.
Algorithm
1
Initialize a variable to hold the sum as 0.2
Loop through each element in the array and add it to the sum.3
Divide the sum by the total number of elements in the array.4
Return or print the result as the average.Code
java
public class AverageArray { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int sum = 0; for (int num : numbers) { sum += num; } double average = (double) sum / numbers.length; System.out.println("Average: " + average); } }
Output
Average: 30.0
Dry Run
Let's trace the array [10, 20, 30, 40, 50] through the code
1
Initialize sum
sum = 0
2
Add first element
sum = 0 + 10 = 10
3
Add second element
sum = 10 + 20 = 30
4
Add third element
sum = 30 + 30 = 60
5
Add fourth element
sum = 60 + 40 = 100
6
Add fifth element
sum = 100 + 50 = 150
7
Calculate average
average = 150 / 5 = 30.0
| Iteration | Current Element | Sum |
|---|---|---|
| 1 | 10 | 10 |
| 2 | 20 | 30 |
| 3 | 30 | 60 |
| 4 | 40 | 100 |
| 5 | 50 | 150 |
Why This Works
Step 1: Sum all elements
We add each number in the array using a loop to get the total sum.
Step 2: Divide by count
We divide the sum by the number of elements using array.length to find the average.
Step 3: Use double for precision
Casting sum to double ensures the average includes decimals if needed.
Alternative Approaches
Using Java Streams
java
import java.util.Arrays; public class AverageArray { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; double average = Arrays.stream(numbers).average().orElse(0); System.out.println("Average: " + average); } }
This method is concise and uses modern Java features but requires Java 8 or higher.
Using traditional for loop with index
java
public class AverageArray { public static void main(String[] args) { int[] numbers = {10, 20, 30, 40, 50}; int sum = 0; for (int i = 0; i < numbers.length; i++) { sum += numbers[i]; } double average = (double) sum / numbers.length; System.out.println("Average: " + average); } }
This is a classic approach using index-based loop, useful if you need the index for other logic.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through all n elements once to sum them, so it runs in O(n) time.
Space Complexity
Only a few variables are used regardless of input size, so space is O(1).
Which Approach is Fastest?
Both the loop and stream methods run in O(n) time; streams add slight overhead but improve readability.
| Approach | Time | Space | Best For |
|---|---|---|---|
| For-each loop | O(n) | O(1) | Simple and clear for beginners |
| Java Streams | O(n) | O(1) | Concise code, modern Java |
| Index-based loop | O(n) | O(1) | When index is needed for extra logic |
Always cast sum to double before division to get a precise average with decimals.
Forgetting to cast sum to double causes integer division, losing decimal precision in the average.