00:00

Java Program to Find the Average of 5 Numbers

Last updated: 3 min read

The average (or mean) of a set of numbers is their sum divided by how many numbers there are:

average = sum of the values / number of values

For 60, 80, 30, 10 and 90, the sum is 270 and there are 5 numbers, so the average is 270 / 5 = 54.

1. Average of 5 Numbers with Fixed Values

The sum is divided by 5.0 rather than 5. If both values were int, Java would do integer division and drop the decimal part.

AverageOfFive.java
public class AverageOfFive {
    public static void main(String[] args) {
        int a = 60, b = 80, c = 30, d = 10, e = 92;

        int sum = a + b + c + d + e;
        double average = sum / 5.0;

        System.out.println("Numbers: " + a + ", " + b + ", " + c + ", " + d + ", " + e);
        System.out.println("Sum: " + sum);
        System.out.println("Average: " + average);
        System.out.println("With integer division: " + (sum / 5));
    }
}
Output
Numbers: 60, 80, 30, 10, 92
Sum: 272
Average: 54.4
With integer division: 54

2. Average of 5 Numbers with User Input

A loop reads the five numbers one by one and adds each to the total.

AverageOfFiveInput.java
import java.util.Scanner;

public class AverageOfFiveInput {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        double sum = 0;

        for (int i = 1; i <= 5; i++) {
            System.out.print("Enter number " + i + ": ");
            sum += scanner.nextDouble();
        }
        scanner.close();

        System.out.printf("Average: %.2f%n", sum / 5);
    }
}
Output (with sample input)
Enter number 1: 12
Enter number 2: 7.5
Enter number 3: 20
Enter number 4: 3
Enter number 5: 9
Average: 10.30

3. Average of an Array of Any Size

Using numbers.length instead of a fixed 5 makes the method work for any array.

AverageOfArray.java
public class AverageOfArray {

    static double average(int[] numbers) {
        int sum = 0;
        for (int n : numbers) {
            sum += n;
        }
        return (double) sum / numbers.length;
    }

    public static void main(String[] args) {
        int[] four = {20, 35, 60, 41};
        int[] six = {10, 20, 30, 40, 50, 30};

        System.out.println("Average of 4 numbers: " + average(four));
        System.out.println("Average of 6 numbers: " + average(six));
    }
}
Output
Average of 4 numbers: 39.0
Average of 6 numbers: 30.0

4. Average Using Java Streams

Since Java 8, Arrays.stream(array).average() calculates the average in one line. It returns an OptionalDouble, because an empty array has no average; orElse(0) supplies a default.

AverageStream.java
import java.util.Arrays;

public class AverageStream {
    public static void main(String[] args) {
        int[] numbers = {60, 80, 30, 10, 90};

        double average = Arrays.stream(numbers).average().orElse(0);

        System.out.println("Average: " + average);
    }
}
Output
Average: 54.0

5. Summary

  • The average is the sum of the values divided by how many values there are.
  • Divide by 5.0 or cast to double to avoid integer division.
  • Use array.length so the same code works for any number of values.
  • Arrays.stream(numbers).average() gives the average in one line.

Related Programs