The average (arithmetic mean) of N numbers is their sum divided by the countN (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
Values
Sum
N
Average
2, 4, 6
12
3
4
10, 20, 30, 40
100
4
25
5
5
1
5
1, 2
3
2
1.5
Approach
How to Find the Average in C#
Four clear steps — sum first, then divide.
Confirm N > 0 (or the array is non-empty).
Read or load the N values.
Accumulate a double running sum.
Return sum / N.
Method
Idea
Best for
Console loop
Read N, then loop N times with a running sum
Interviews, interactive demos
Array helper
Sum every element, divide by Length
Reusable 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
Goal
Pattern
Formula
average = sum / N
Safe divide
Require N > 0 before dividing
Decimals
Use double sum, then sum / n (not int / int)
LINQ
values.Average() — empty throws
Try it
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
Trace
Worked Walkthrough — 10, 20, 30, 40
Running sum starts at 0. Add each value once, then divide by the count.
Step
Value
Running sum
1
10
10
2
20
30
3
30
60
4
40
100
Final: N = 4, average = 100 / 4 = 25.
Code
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);
}
}
Output (sample: N=4, values 10 20 30 40)
How many numbers (N)? 4
Enter number 1: 10
Enter number 2: 20
Enter number 3: 30
Enter number 4: 40
Sum = 100
Average = 25
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);
}
}
Output
Average = 25
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());
}
}
Output
25
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.
Analysis
Time and Space Complexity
Program
Time
Extra space
Console running sum
O(N)
O(1)
Array helper
O(N)
O(1) beyond the input array
LINQ Average()
O(N)
O(1) typical
Remember
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.