Answer to Question #289268 in C++ for hammad

Question #289268

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


1
Expert's answer
2022-01-21T01:46:26-0500
#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;
}

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