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

Program to count the number of even and odd elements in an array

Here we will going to learn about the process of counting the number of even and odd elements in an array which is discussed here. 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

output-

even elements-2

odd elements- 2

Algorithm:-

  1. Input the number of elements of the array.
  2. Input the array elements.
  3. Initialize count_odd = count_even = 0.
  4. Traverse the array and increment count_odd if the array element is odd, else increment count_even.
  5. Print count_odd and count_even.

C++ Code:-

     
     #include<iostream>
     using namespace std;
     void main(String[] args)
     {
          int m;
          cin>>m;
          int*arr = new int[m];
          for(int i = 0; i < m; i++)
          {
               cin>>arr[i];
          }
          int count_odd =0, count_even = 0;
          for(int i = 0; i < m; i++)
          {
               if(arr[i] % 2 == 1)
                    odd++;
               else
                    even++;
          }
          cout<<“Odd:  “<<odd;
          cout<<“Even:  “<<even;
     }
    
    output
    Enter size of an array-6
    Enter elements of an array
    12
    3
    4
    7
    8
    9
    Even elemenets-3
    Odd elements –3