Answer to Question #318473 in C++ for Four 4

Question #318473

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.



The loop shall only be terminated when the inputted integer is zero. Afterwards, print out the total of all inputted positive integers.

1
Expert's answer
2022-03-26T05:45:25-0400
#include <iostream>


int main() {
    int total = 0;


    int n;
    do {
        std::cin >> n;
        total += n > 0 ? n : 0;
    } while (n != 0);
    std::cout << total;
    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