Sentinel Loop

Lab C++ Intermediate 20 points

Read integers until 0. Print the sum and how many numbers were read (not counting the 0), using a do-while loop.

Input

Integers, the last is 0.

Output

Two lines.

Example

Input

5 -3 8 0

Output

10
3
Sample input available Sample output available
Editor
C++

Hints

The 0 ends the input but isn't counted.

Solution explanation

A sentinel value tells the program where the input stops.

Show solution
#include <iostream>
using namespace std;

int main() {
    int x, count = 0;
    long long sum = 0;
    do {
        cin >> x;
        if (x != 0) {
            sum += x;
            count++;
        }
    } while (x != 0);
    cout << sum << "\n" << count << "\n";
    return 0;
}
Output
Answer
HOME LEARN COMMUNITY DASHBOARD