C# Average of N Numbers Program

Beginner
6 min read
Updated: Sep 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 N (when N > 0).

Remember
average = (x1 + x2 + … + xN) / N
        = sum / N

N must be greater than 0
Use floating-point division (double) for decimals

Example: 10, 20, 30, 40
sum = 100, N = 4 → average = 25
ValuesSumNAverage
2, 4, 61234
10, 20, 30, 40100425
5515
1, 2321.5

How to Find the Average in C#

Four clear steps — sum first, then divide.

  1. Confirm N > 0 (or the array is non-empty).
  2. Read or load the N values.
  3. Accumulate a double running sum.
  4. Return sum / N.
MethodIdeaBest for
Console loopRead N, then loop N times with a running sumInterviews, interactive demos
Array helperSum every element, divide by LengthReusable library-style code
LINQ Average()values.Average()Short application code (guard empty)

Pseudocode

Pseudocode
function Average(values):
    n = length of values
    if n == 0:
        return error   // or a documented sentinel

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

    return sum / n

Cheat sheet

GoalPattern
Formulaaverage = sum / N
Safe divideRequire N > 0 before dividing
DecimalsUse double sum, then sum / n (not int / int)
LINQvalues.Average() — empty throws

Live Preview

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

Separate values with commas. Empty lists are rejected (no division by zero).

Result
Values: 10, 20, 30, 40
N = 4
Sum = 100
Average = 100 / 4 = 25

Worked Walkthrough — 10, 20, 30, 40

Running sum starts at 0. Add each value once, then divide by the count.

StepValueRunning sum
11010
22030
33060
440100

Final: N = 4, average = 100 / 4 = 25.

C# Programs

Three programs: Console input loop, array helper, and LINQ Average(). Use View Output for sample results.

Example 1 — Average from Console Input

Read N, then loop N times accumulating a double sum.

C#
using System;

class Program
{
    static void Main()
    {
        Console.Write("How many numbers (N)? ");
        if (!int.TryParse(Console.ReadLine(), out int n) || n <= 0)
        {
            Console.WriteLine("N must be a positive integer.");
            return;
        }

        double sum = 0;
        for (int i = 1; i <= n; i++)
        {
            Console.Write("Enter number " + i + ": ");
            if (!double.TryParse(Console.ReadLine(), out double value))
            {
                Console.WriteLine("Invalid number.");
                return;
            }
            sum += value;
        }

        double average = sum / n;
        Console.WriteLine("Sum = " + sum);
        Console.WriteLine("Average = " + average);
    }
}

How It Works

1. Guard N. Reject n <= 0 so you never divide by zero.

2. Running sum. Each valid input adds into sum. Extra space stays O(1).

3. Divide once. sum / n uses double, so results like 1.5 stay exact enough for demos.

Example 2 — Average from an Array

Reusable helper: sum every element, then divide by Length.

C#
using System;

class Program
{
    static double AverageFromArray(double[] values)
    {
        if (values == null || values.Length == 0)
        {
            return 0.0; // documented sentinel for empty input
        }

        double sum = 0;
        for (int i = 0; i < values.Length; i++)
        {
            sum += values[i];
        }

        return sum / values.Length;
    }

    static void Main()
    {
        double[] data = { 10, 20, 30, 40 };
        double avg = AverageFromArray(data);
        Console.WriteLine("Average = " + avg);
    }
}

How It Works

1. Empty guard. Return a documented sentinel (here 0.0) instead of dividing by zero.

2. One pass. A single loop builds the sum — O(N) time.

3. Same formula. sum / values.Length is the arithmetic mean. Easy to unit-test separately from Console I/O.

Example 3 — LINQ Average()

Short application style — still guard empty sequences first.

C#
using System;
using System.Linq;

class Program
{
    static void Main()
    {
        double[] data = { 10, 20, 30, 40 };

        if (data.Length == 0)
        {
            Console.WriteLine("No values to average.");
            return;
        }

        Console.WriteLine(data.Average());
    }
}

How It Works

1. Same math. Average() still computes sum / count under the hood.

2. Empty throws. Without the length check, an empty array makes Average() throw InvalidOperationException.

3. Interview tip. Explain the manual loop first; mention LINQ as a concise production option.

Edge Cases & Pitfalls

Check these before calling the solution done.

N = 0

No divide by zero

Reject empty counts before computing the mean.

int / int

Truncation

10 / 4 is 2 in integers. Use double for decimals.

N = 1

Average = itself

A single value’s average is that value.

Negatives

Allowed

The formula works with negative numbers too.

Median

Different metric

Average ≠ median. Do not sort for the arithmetic mean.

LINQ empty

Guard first

Average() throws on an empty sequence.

Time and Space Complexity

ProgramTimeExtra space
Console running sumO(N)O(1)
Array helperO(N)O(1) beyond the input array
LINQ Average()O(N)O(1) typical

Key Takeaways

  • Formula: average = sum / N (arithmetic mean).
  • Guard N: never divide when the count is zero.
  • Use double: avoid integer truncation on the divide.
  • Test set: 10,20,30,40 → 25; 1,2 → 1.5; empty → reject.

One line: sum the values, divide by the count — and never divide by zero.

Frequently Asked Questions

Add all N numbers, then divide by N. Formula: average = sum / N. Use double so the result keeps decimals.
Division by zero is undefined. If the count is 0, do not compute an average.
Prefer double (or decimal) for the sum and the result. Integer division truncates — for example 10 / 4 becomes 2, not 2.5.
No. Average is sum/count. Median is the middle value after sorting.
Yes. values.Average() returns the arithmetic mean. Guard empty arrays first — Average() throws on an empty sequence.
Do not divide. Return an error message or a documented sentinel. Demos in this tutorial return 0.0 for empty arrays.
O(N) — you visit each number once to build the sum. Extra space is O(1) for a running sum, or O(N) if you store the values in an array.
Read N with int.TryParse, then loop N times reading each value into a running double sum, then divide by N.
No for the arithmetic mean. Summing in any order gives the same average (ignoring floating-point rounding).
If N = 1, the average equals that number itself.

Did you know?

Average is the arithmetic mean: sum divided by count. If the sum is 100 and N is 5, the average is 20.

Next: Biggest of Three Numbers

Compare three values and print the largest.

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.

12 people found this page helpful