C++ Program to Calculate the Sum and the Average of N Numbers

Average of n numbers

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. C++ Program & output to calculate sum & average of n numbers without array

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

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    int i, n, num, sum = 0;
    float avg;

    // take input of number of elements
    cout << "Enter no of elements: ";
    cin >> n;

    // take input of numbers one by one
    // add each number to sum
    cout << "Enter " << n << " elements: ";
    for (i = 1; i <= n; i++) {
        cin >> num;
        sum = sum + num;
    }

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

    // display result
    cout << "Sum: " << sum << endl;
    cout << "Average: " << avg << endl;

    return 0;
}

Output


Enter no of elements: 7

Enter 7 elements: 12 9 4 10 5 3 5

Sum: 48

Average: 6.85714





5. 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
*********************************/

#include<iostream>

using namespace std;

int main() {
    // delclare variables
    int i, n, N[20] = {0}, sum = 0;
    float avg;

    // take input of number of elements
    cout << "Enter no of elements: ";
    cin >> n;

    // take input and store into array
    cout << "Enter " << n << " elements: ";
    for (i = 0; i < n; i++) {
        cin >> N[i];
    }

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

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

    // display result
    cout << "Sum: " << sum << endl;
    cout << "Average: " << avg << endl;

    return 0;
}

Output


Enter no of elements: 5

Enter 5 elements: 17 30 15 6 12

Sum: 80

Average: 16