Write a function that will return count of even number and the odd number in an array of int? for example : when call the function in the main for the following array: int a[5]={2,5,9,-8,85}; and the output is even=2 odd=3
ok, read on:
* Any number that gives you a 0 as remainder when divided by 2 is considered an even number OTHERWISE it is an odd number.
in c++, the modulus (%) operator makes this check simple for us. For example 4%2 = 0 as a remainder, so it is an even number
while 7 % 2 = 1 as a remainder , so it is an odd number.
#include <iostream>
usingnamespace std;
void checkNumber(int arr[], int arraySize, int &numOfEven, int &numOfOdd)
{
for(int x = 0; x < arraySize; ++x)
{
if(arr[x] % 2)
{
// if element at index x divided by 2 gives me a non-zero number,
// then it is an odd number
++numOfOdd;
}
else
{
// otherwise it is an even number
++numOfEven;
}
}
}
int main()
{
constint arraySize = 5;
int arr[arraySize] = {4, 43, 4, 8, 2};
int numOfEven = 0;
int numOfOdd = 0;
checkNumber(arr, arraySize, numOfEven, numOfOdd);
cout << "Number Of Even: " << numOfEven << endl;
cout << "Number Of Odd: " << numOfOdd << endl;
return 0;
}