Question #283710

4. Gathering Positivity

by CodeChum Admin

I always want to look at the positive side of things, so I decide to seriously look at positive numbers, too!


Will you code along with me?



Instructions:

  1. Using the do…while() loop, continuously scan for random integers, but add up only all the positive integers and store the total in one variable.
  2. The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.

Input

Multiple lines containing an integer on each.

2
3
4
-1
-5
1
0

Output

A line containing an integer.


10

Expert's answer

#include <iostream>


using namespace std;


int main()
{
    int n, ans = 0;
    do
    {
        cin >> n;
        if (n > 0) ans++;
    } while (n != 0);
    cout << ans << '\n';
    


    return 0;
}
LATEST TUTORIALS
APPROVED BY CLIENTS