C# Program to Find Average of Numbers
To find the average of numbers in C#, sum all numbers using
sum += number and then divide by the count using average = sum / count.Examples
Inputnumbers = [5]
OutputAverage is 5
Inputnumbers = [2, 4, 6, 8]
OutputAverage is 5
Inputnumbers = [0, 0, 0, 0]
OutputAverage is 0
How to Think About It
To find the average, first add all the numbers together to get a total sum. Then count how many numbers there are. Finally, divide the total sum by the count to get the average value.
Algorithm
1
Get the list of numbers2
Add all numbers to find the total sum3
Count how many numbers are in the list4
Divide the total sum by the count to get the average5
Display the averageCode
csharp
using System; class Program { static void Main() { int[] numbers = {2, 4, 6, 8}; int sum = 0; foreach (int num in numbers) { sum += num; } double average = (double)sum / numbers.Length; Console.WriteLine("Average is " + average); } }
Output
Average is 5
Dry Run
Let's trace the example numbers = [2, 4, 6, 8] through the code
1
Initialize sum
sum = 0
2
Add first number
sum = 0 + 2 = 2
3
Add second number
sum = 2 + 4 = 6
4
Add third number
sum = 6 + 6 = 12
5
Add fourth number
sum = 12 + 8 = 20
6
Calculate average
average = 20 / 4 = 5
7
Print result
Output: Average is 5
| Iteration | Number Added | Sum After Addition |
|---|---|---|
| 1 | 2 | 2 |
| 2 | 4 | 6 |
| 3 | 6 | 12 |
| 4 | 8 | 20 |
Why This Works
Step 1: Summing numbers
We use sum += number to add each number to the total sum.
Step 2: Counting numbers
We find how many numbers there are using numbers.Length.
Step 3: Calculating average
We divide the total sum by the count with average = (double)sum / numbers.Length to get the average as a decimal.
Alternative Approaches
Using LINQ Average() method
csharp
using System; using System.Linq; class Program { static void Main() { int[] numbers = {2, 4, 6, 8}; double average = numbers.Average(); Console.WriteLine("Average is " + average); } }
This method is shorter and uses built-in LINQ but requires <code>using System.Linq;</code>.
Using for loop instead of foreach
csharp
using System; class Program { static void Main() { int[] numbers = {2, 4, 6, 8}; int sum = 0; for (int i = 0; i < numbers.Length; i++) { sum += numbers[i]; } double average = (double)sum / numbers.Length; Console.WriteLine("Average is " + average); } }
This uses a classic for loop instead of foreach, useful if you need the index.
Complexity: O(n) time, O(1) space
Time Complexity
The program loops through all numbers once to sum them, so time grows linearly with input size.
Space Complexity
Only a few variables are used regardless of input size, so space is constant.
Which Approach is Fastest?
Using LINQ's Average() is concise but internally also loops once, so performance is similar to manual sum and divide.
| Approach | Time | Space | Best For |
|---|---|---|---|
| Manual sum with foreach | O(n) | O(1) | Clear step-by-step control |
| LINQ Average() | O(n) | O(1) | Concise and readable code |
| For loop sum | O(n) | O(1) | When index access is needed |
Cast sum to double before division to get a precise average with decimals.
Forgetting to cast to double causes integer division, which drops decimals in the average.