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
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;
}
Comments
Leave a comment