Answer to Question #161236 in C++ for Ashes

Question #161236

Write a C++ program that takes 20 integer inputs from user and calculates and displays the

following:

 Number of positive numbers

 Number of negative numbers

 Number of odd numbers

 Number of even numbers

 Number of zeros


1
Expert's answer
2021-02-08T05:09:07-0500

Reading 20 integer values from the console, during reading, we count the number of positive numbers, then the number of negative numbers will be equal to 20 minus the number of positive numbers, we also count even numbers and zeros.


#include <iostream>

using namespace std;

int main()
{
    int positive = 0;
    int even = 0;
    int zero = 0;
    for (int i = 0; i < 20; i++) {
        int num = 0;
        cin >> num;
        if (num >= 0) {
            positive++;
        }
        if (num % 2 == 0) {
            even++;
        }
        if (num == 0) {
            zero++;
        }
    }
    
    cout << "Number of positive numbers: " << positive << endl;
    cout << "Number of negative numbers: " << 20 - positive << endl;
    cout << "Number of odd numbers: " << 20 - even << endl;
    cout << "Number of even numbers: " << even << endl;
    cout << "Number of zeros: " << zero << 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