Answer to Question #349680 in C++ for cheche

Question #349680

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
2022-06-10T08:30:16-0400
#include <iostream>
#include <ctime>
#include <stdlib.h>

using namespace std;
int main() {
 
    int sum=0, n;
    srand(time(NULL));  
        
    n = rand()%20-10;    // Random int
    cout<<n<<endl;
    if(n>0)
        sum=n;             
    while(n!=0) {
        n = rand()%20-10;    // Random int
        if(n>0)
          sum=sum+n;
        cout<<n<<endl;     
    }    
    
      cout<<endl << sum<<endl;    
 
    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
APPROVED BY CLIENTS