Answer to Question #283710 in C++ for Collay

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
1
Expert's answer
2021-12-30T07:13:40-0500
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog