In: Computer Science
in C++, Write a program that asks the user to enter 6 numbers. Use an array to store these numbers. Your program should then count the number of odd numbers, the number of even numbers, the negative, and positive numbers. At the end, your program should display all of these counts. Remember that 0 is neither negative or positive, so if a zero is entered it should not be counted as positive or negative. However, 0 is an even number.
Sample Run:
Enter 10 numbers 2 3 -3 6 -1 0 You entered: 3 odd numbers 3 even numbers 2 negative numbers 3 positive numbers
Program:
#include <iostream>
using namespace std;
int main()
{
int n = 6, num_array[6];
int even_count = 0, odd_count = 0;
int positive_count = 0, negative_count = 0;
cout << "Enter 6 numbers" << endl;
for (int i = 0; i < n;i++) {
// Taking input 6 numbers and adding to array
cin >> num_array[i];
// If condition to check positive number
if (num_array[i] > 0) {
positive_count++; // Increment positive count
} else if (num_array[i] < 0) { // condition to check negative number
negative_count++; // Increment negative count
}
// If condition to check for even number
if (num_array[i]%2 == 0) {
even_count++; //Increment even count
} else { // If number is not even
odd_count++; // Increment odd count
}
}
// Printing output count of odd,even,negative,positive numbers
cout << odd_count << " odd numbers" << endl;
cout << even_count << " even numbers" << endl;
cout << negative_count << " negative numbers" << endl;
cout << positive_count << " positive numbers" << endl;
return 0;
}
Output: