C++ program to count numbers of even and odd elements in an array

Count numbers of even and odd elements

Here we will going to learn about the process of counting numbers of even and odd elements. Given an array of integers, count the total number of odd elements and even elements in the array and display them as output.

Example

Input – 1,2,3,4,5

output-

even elements-2

odd elements- 3

count numbers of even and odd elements
count numbers of even and odd elements

Algorithm

  • Input the number of elements of the array.
  • Input the array elements.
  • Initialize count_odd = count_even = 0.
  • Traverse the array and increment count_odd if the array element is odd, else increment count_even.
  • Print count_odd and count_even.

C++  code (count numbers of even and odd elements)

#include <iostream>

using namespace std;
int main()
{
int a[1000],i,n,even=0,odd=0;

cout<<"Enter size of the array : ";
cin>>n;

cout<<"Enter elements in array : ";
for(i=0; i<n; i++)
{
cin>>a[i];
}

for(i=0; i<n; i++)
{
if(a[i]%2==0)
even++;
else
odd++;

}

cout<<"even numbers in array:"<<even;
cout<<"\n odd numbers in array:"<<odd;


return 0;
}

Output:-

Enter size of the array : 5
Enter elements in array : 1
2
3
4
4
even numbers in array:3
odd numbers in array:2