C# Average of N Numbers Program

Beginner
6 min read
Updated: Oct 2026
2 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

Pseudocode

Pseudocode
function Average(values):
    n = length of values
    if n == 0:
        raise an error

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

    return sum / n

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

Add each value once, then divide the sum by the count.

Calculation
Values:  10, 20, 30, 40
Sum:     10 + 20 + 30 + 40 = 100
Count:   N = 4
Average: 100 / 4 = 25

C# Programs

Start with Console input, then use a reusable array helper.

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: reject a non-positive count, add each valid number to a double sum, and divide once after the loop.

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)
        {
            throw new ArgumentException("Values cannot be empty.");
        }

        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: reject null or empty arrays, add every element in one pass, and divide by values.Length.

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.

Time and Space Complexity

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

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.
Use double or decimal for the sum and result. Integer division truncates the decimal part, so 10 / 4 would become 2 instead of 2.5.
Yes. values.Average() returns the arithmetic mean, but it throws InvalidOperationException when the sequence is empty.
The program takes O(N) time because it visits each number once. A running sum uses O(1) extra space.

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