Java Average of N Numbers Program

Beginner
5 min read
Updated: Oct 2026
3 programs
Live preview

What Is the Average of N Numbers?

The average (arithmetic mean) of N numbers is their sum divided by the count.

Remember
average = (a₁ + a₂ + … + aₙ) / N

Example: 1, 2, 3, 4, 5
  sum = 15, N = 5
  average = 15 / 5 = 3
ValuesSumNAverage
1, 2, 3, 4, 51553
10, 15, 20, 25, 30100520
2, 4623
7717

How to Find the Average of N Numbers in Java

Sum every value once, then divide by the count — guard empty input first.

  1. Reject N <= 0 (no division by zero).
  2. Read or iterate the N values.
  3. Accumulate into a double sum.
  4. Return sum / N.
MethodIdeaTimeBest for
User input loopScanner N times, running sumO(N)Console demos
Array helperSum array, then divideO(N)Interviews, default
Compact mainInline sum in mainO(N)Tiny samples

Pseudocode

Pseudocode
function Average(values, N):
    if N <= 0:
        return error   // or a documented sentinel

    sum = 0
    for each value in values:
        sum = sum + value

    return sum / N

Cheat Sheet

Keep these facts handy while writing the program.

TopicDetail
Formulaaverage = sum / N
TypeUse double for fractional means
GuardNever divide when N <= 0
Not medianMedian needs sorting; average does not
1..5sum 15 → average 3

Live Preview

Enter comma-separated numbers to see the sum, count, and average.

Comma-separated numbers. Tap a chip or type your own list.

Result
values = [1, 2, 3, 4, 5]
sum = 15, N = 5
average = 3

Worked Walkthrough — 1, 2, 3, 4, 5

Add every value, then divide by the count.

Calculation
values: 1, 2, 3, 4, 5
N = 5

sum = 0
  + 1 → 1
  + 2 → 3
  + 3 → 6
  + 4 → 10
  + 5 → 15

average = 15 / 5 = 3

Java Programs

Start with console input, then an array helper, and finish with a compact sum loop in main.

Example 1 — Average from User Input

Validate N, loop N times with Scanner, keep a running total, then divide.

Java
import java.util.Scanner;

class AverageInput {
    static double averageOfNNumbers(int n, Scanner sc) {
        if (n <= 0) {
            return 0.0;
        }

        double total = 0.0;
        System.out.println("Enter " + n + " numbers:");
        for (int i = 0; i < n; i++) {
            total += sc.nextDouble();
        }

        return total / n;
    }

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int n = 5;
        double result = averageOfNNumbers(n, sc);
        System.out.println("The average of the entered numbers is: " + result);
        sc.close();
    }
}

How It Works

  1. Return 0.0 if n <= 0 (documented sentinel).
  2. Prompt and read each value with sc.nextDouble().
  3. Add every value into a double running total.
  4. Divide by n so the mean keeps decimals.
  5. Sample input 1…5 prints 3.0.

Example 2 — Average from an Array

Loop the array once — short and interview-friendly.

Java
class AverageArray {
    static double averageFromArray(double[] values) {
        if (values == null || values.length == 0) {
            return 0.0;
        }

        double total = 0.0;
        for (int i = 0; i < values.length; i++) {
            total += values[i];
        }
        return total / values.length;
    }

    public static void main(String[] args) {
        double[] data = { 10, 15, 20, 25, 30 };
        double avg = averageFromArray(data);
        System.out.println("Average = " + avg);
    }
}

How It Works

  1. Guard null or empty arrays with a sentinel return.
  2. Sum every element in one forward pass.
  3. Divide by values.length for the arithmetic mean.
  4. The array length is the count — no separate N variable needed.
  5. 10+15+20+25+30 = 100 → average 20.

Example 3 — Compact Sum Loop

A tiny main that sums a fixed array — useful when values are already known.

Java
class AverageCompact {
    public static void main(String[] args) {
        double[] data = { 1, 2, 3, 4, 5 };
        double sum = 0.0;

        if (data.length == 0) {
            System.out.println("No average for an empty array.");
        } else {
            for (int i = 0; i < data.length; i++) {
                sum += data[i];
            }
            System.out.println(sum / data.length);
        }
    }
}

How It Works

  1. Detect an empty array before any division.
  2. Sum with a plain for loop.
  3. Print sum / data.length.
  4. Same math as the helper — just all in main.
  5. Good for quick demos; prefer a helper in interviews.

Edge Cases & Pitfalls

Check these before calling the solution done.

Zero

Never divide by N = 0

Empty input is undefined. Return an error or a documented sentinel.

int / int

Avoid integer division

10 / 4 is 2 in integer math. Use double for the sum or cast before dividing.

Overflow

Sum can grow large

Prefer double when values or N are large.

Median

Different from median

Average is sum/count. Median needs a sorted middle value.

Time and Space Complexity

ProgramTimeExtra space
Input / array averageO(N)O(1) beyond the data
Storing all inputsO(N)O(N)
Running sum onlyO(N)O(1)

One pass is enough. You do not need to sort for an arithmetic mean.

Key Takeaways

  • Formula: average = sum / N.
  • Guard: never divide when N is zero.
  • Type: use double for fractional results.
  • Test: 1…5 → 3; 10…30 step 5 → 20.

One line: add every value, divide by the count — that is the average.

Frequently Asked Questions

Add all N numbers, then divide by N. Formula: average = sum / N.
Division by zero is invalid. If N is 0, the average is undefined.
Use double for the sum and result when you want decimals. Integer division truncates (10 / 4 becomes 2).
O(N), because each number is visited once to build the sum.
No. Average is sum/count. Median is the middle value after sorting.

Next: Biggest of Three Numbers

Find the largest among three values with simple comparisons.

Biggest of three tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

18 people found this page helpful