Answer to Question #286664 in C++ for Lilie

Question #286664

Create a C++ program that will accept input of any number then press 0 to stop. Count and display the positive and negative numbers.

1
Expert's answer
2022-01-11T11:06:27-0500
#include <iostream>
using namespace std;

int main() {
    int num_pos = 0, num_neg = 0, x;

    cin >> x;
    while (x) {
        if ( x > 0)
            num_pos++;
        else
            num_neg++;
        cin >> x;
    }

    cout << "There were " << num_pos << " positive and "
         << num_neg << " negative numbers." << 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