C# programs to calculate the sum and the average of N numbers have been shown here. For example, if the numbers are 10, 20, 30, 40, 50, then the sum of these numbers would be 10 + 20 + 30 + 40 + 50 = 150 and average would be (150 / 5) = 30. The result can be obtained using array and also without using array.

## 1. Algorithm to find the sum and average of N numbers

1. Take number of terms $n$ as inputs.

2. Set $s = 0$ and $i = 0$

3. Take a number $x$ as input.

4. Perform $s = s + x$ and $i = i + 1$

5. Check if $i < n$, then go to step 3, else go to step 6

6. Display $s$ as the sum and $\frac{s}{n}$ as the average and exit program.

## 2. Pseudocode to find the sum and average of N numbers

Input: Number of terms $n$

Output: Sum and average of those $n$ terms

1. Procedure sumAverage($n$):

2. $sum \leftarrow 0$

3. $i \leftarrow 0$

4. Repeat for each $i < n$:

5. Read a number $x$

6. $sum \leftarrow sum + x$

7. $i \leftarrow i + 1$

8. $avg \leftarrow \frac{sum}{n}$

9. Return $sum$, $avg$

10. End Procedure

## 3. Time complexity to find the sum and average of N numbers

Time Complexity: O(n)

Here $n$ is the number of elements.

## 4.1. C# Program & output to calculate sum & average of n numbers without using array

Code has been copied
/***********************************
alphabetacoder.com
C# Program to calculate sum and
average of n numbers
************************************/

using System;

namespace SumAverage {
class Program {
static void Main() {
// delclare variables
int i, n, num, sum = 0;
float avg;

// take input of number of elements
Console.Write("Enter no of elements: ");
n = Convert.ToInt32(Console.ReadLine());

// take input of numbers one by one
// add each number to sum
Console.WriteLine("Enter " + n + " elements: ");
for (i = 1; i <= n; i++) {
num = Convert.ToInt32(Console.ReadLine());
sum = sum + num;
}

// calculate average
avg = (float) sum / n;

// display result
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Average: " + avg);

// wait for user to press any key
Console.ReadKey();
}
}
}


Output

Enter no of elements: 6

Enter 6 elements:

10

17

11

6

9

5

Sum: 58

Average: 9.666667

## 4.2. C# Program to calculate sum & average of n numbers using array

Code has been copied
/***********************************
alphabetacoder.com
C# Program to calculate sum and
average of n numbers using array
************************************/

using System;

namespace SumAverage {
class Program {
static void Main() {
// delclare variables
int i, n, sum = 0;
int[] N = new int[20];
float avg;

// take input of number of elements
Console.Write("Enter no of elements: ");
n = Convert.ToInt32(Console.ReadLine());

// take input of numbers and store in the array
Console.WriteLine("Enter " + n + " elements: ");
for (i = 0; i < n; i++) {
N[i] = Convert.ToInt32(Console.ReadLine());
}

// perform sum of array elements
for (i = 0; i < n; i++) {
sum = sum + N[i];
}

// calculate average
avg = (float) sum / n;

// display result
Console.WriteLine("Sum: " + sum);
Console.WriteLine("Average: " + avg);

// wait for user to press any key
Console.ReadKey();
}
}
}


Output

Enter no of elements: 4

Enter 4 elements:

12

5

6

3

Sum: 26

Average: 6.5