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
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
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
Add each value once, then divide the sum by the count.
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);
}
}
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: 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);
}
}
Output
Average = 25
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.
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
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.