0
0
JavaProgramBeginner · 2 min read

Java Program to Find Average of Numbers

To find the average of numbers in Java, sum all the numbers using a loop and then divide the total by the count using average = sum / count.
📋

Examples

Inputnumbers = {10, 20, 30}
OutputAverage is 20.0
Inputnumbers = {5, 15, 25, 35}
OutputAverage is 20.0
Inputnumbers = {100}
OutputAverage is 100.0
🧠

How to Think About It

To find the average, first add all the numbers together to get the total sum. Then count how many numbers there are. Finally, divide the total sum by the count to get the average value.
📐

Algorithm

1
Get the list of numbers.
2
Add all numbers to find the total sum.
3
Count how many numbers are in the list.
4
Divide the total sum by the count to get the average.
5
Print the average.
💻

Code

java
public class AverageCalculator {
    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 is " + average);
    }
}
Output
Average is 30.0
🔍

Dry Run

Let's trace the example numbers = {10, 20, 30, 40, 50} through the code

1

Initialize sum

sum = 0

2

Add first number

sum = 0 + 10 = 10

3

Add second number

sum = 10 + 20 = 30

4

Add third number

sum = 30 + 30 = 60

5

Add fourth number

sum = 60 + 40 = 100

6

Add fifth number

sum = 100 + 50 = 150

7

Calculate average

average = 150 / 5 = 30.0

8

Print average

Output: Average is 30.0

IterationNumber AddedSum After Addition
11010
22030
33060
440100
550150
💡

Why This Works

Step 1: Summing numbers

The code uses a loop to add each number to the total sum using sum += num.

Step 2: Calculating average

The average is found by dividing the sum by the count of numbers with average = (double) sum / numbers.length to get a decimal result.

Step 3: Printing result

The program prints the average using System.out.println to show the final output.

🔄

Alternative Approaches

Using Scanner for user input
java
import java.util.Scanner;
public class AverageCalculator {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("Enter number of elements: ");
        int n = scanner.nextInt();
        int sum = 0;
        for (int i = 0; i < n; i++) {
            System.out.print("Enter number " + (i+1) + ": ");
            sum += scanner.nextInt();
        }
        double average = (double) sum / n;
        System.out.println("Average is " + average);
        scanner.close();
    }
}
This approach allows dynamic input from the user but requires more code and input handling.
Using Java Streams
java
import java.util.Arrays;
public class AverageCalculator {
    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 is " + average);
    }
}
This method is concise and uses modern Java features but requires Java 8 or higher.

Complexity: O(n) time, O(1) space

Time Complexity

The program loops through all numbers once to sum them, so time grows linearly with input size.

Space Complexity

Only a few variables are used regardless of input size, so space is constant.

Which Approach is Fastest?

The simple loop and the streams approach both run in O(n) time; streams add some overhead but improve readability.

ApproachTimeSpaceBest For
Simple loopO(n)O(1)Basic and clear for beginners
User input with ScannerO(n)O(1)Interactive programs needing user input
Java StreamsO(n)O(1)Concise code with modern Java features
💡
Cast the sum to double before division to get an accurate decimal average.
⚠️
Forgetting to cast to double causes integer division, which truncates the decimal part.