Declare an Array of 20 integers. Initialize the array with random integers using the rand() function and print the following: a. number of positive numbers b. number of negative numbers c. number of odd numbers d. number of even numbers e. number of 0
#include <iostream>
#include <cmath>
using namespace std;
int main() {
const int N=20;
int array[N];
for (int i=0; i<N; i++) {
array[i] = rand();
}
int pos_count = 0;
int neg_count = 0;
int odd_count = 0;
int even_count = 0;
int zero_count = 0;
for (int i=0; i<N; i++) {
if (array[i] > 0) {
pos_count++;
}
if (array[i] < 0) {
neg_count++;
}
if (array[i] == 0) {
zero_count++;
}
if (array[i] % 2 == 0) {
even_count++;
}
if (array[i] % 2 != 0) {
odd_count++;
}
}
cout << "There are " << pos_count << " positive number." << endl;
cout << "There are " << neg_count << " negative number." << endl;
cout << "There are " << odd_count << " odd number." << endl;
cout << "There are " << even_count << " even number." << endl;
cout << "There are " << zero_count << " zeros." << endl;
return 0;
}
Comments
Leave a comment