C++ Program to Find the Average and Sum of Numbers Until 0 is Entered

Add numbers until 0

C++ programs to find the sum and average of the numbers until 0 is entered, are given here. Suppose the input numbers are 5, 10, 9, 0, then the sum = (5 + 10 + 9) = 24 and average = (5 + 10 + 9) / 3 = 8.







1. Algorithm to find the sum and average of the numbers until 0 is entered


1. Set sum = 0, count = 0.

2. Take a number n as input.

3. Perform sum = sum + n.

4. Check if n = 0

5. If step 4 is true declare sum and as the sum and (sum / count) as the average of the numbers entered.

6. If step 4 is false, perform count = count + 1 and go to step 2




2. Pseudocode to find the sum and average of the numbers until 0 is entered


1. Procedure sumAverage():

2. sum := 0

3. count := 0

4. n := Input()

5. Repeat until n = 0

6. sum := sum + n

7. count := count + 1

8. n := Input()

9. avg := (sum / count)

10.Return sum, avg

11. End Procedure





3. Time complexity to find the sum and average of the numbers until 0 is entered


Time Complexity: O(n)

Here n is the number of terms.





4. Program to find the sum and average of the numbers until 0 is entered




4.1. C++ Program to find the sum and average of the numbers until 0 is entered using iteration

Code has been copied
/*********************************
	 alphabetacoder.com
C++ program to find average and
sum of numbers until 0 is entered
**********************************/

#include <iostream>

using namespace std;

int main() {
    // declare variables
    int n, count, sum;

    // initialize
    sum = 0;
    count = -1;

    // keep taking input 
    // until 0 is entered
    do {
        // take input
        cout << "Enter a number: ";
        cin >> n;

        // add to sum
        sum = sum + n;

        // increment the value of number count
        count++;

    } while (n != 0);

    // display the sum and average
    cout << "Total number entered: " << count << endl;
    cout << "Sum: " << sum << endl;
    cout << "Average: " << (double) sum / count << endl;

    return 0;
}

Output


Enter a number: 4

Enter a number: 6

Enter a number: 7

Enter a number: 10

Enter a number: 9

Enter a number: 0


Total number entered: 5

Sum: 36

Average: 7.2




4.2. C++ Program to find the sum and average of the numbers until 0 is entered using recursion

Code has been copied
/*******************************************
	     alphabetacoder.com
C++ program to find sum and average of 
numbers until 0 is entered using recursion
********************************************/

#include <iostream>

using namespace std;

void findSumAverage(int sum, int count) {
    // take input
    int n;
    cout << "Enter a number: ";
    cin >> n;

    // keep taking input 
    // until 0 is entered
    if (n == 0) {
        cout << "Total number entered: " << count << endl;
        cout << "Sum: " << sum << endl;
        cout << "Average: " << (double) sum / count << endl;
    } else {
        // add input to sum and increment count
        sum += n;
        count++;
        findSumAverage(sum, count); // recursive call
    }
}

int main() {
    // declare and initialize variables
    int sum = 0, count = 0;

    // call function to display result
    findSumAverage(sum, count);

    return 0;
}

Output


Enter a number: 10

Enter a number: 15

Enter a number: 22

Enter a number: 10

Enter a number: 4

Enter a number: 0


Total number entered: 5

Sum: 61

Average: 12.2