Count number of Even element and Odd elements of the Array

Count number of Even element and Odd elements of the Array” is an elementary problem of array data structure for beginners. Here, we are given an array of size ‘n’ and our task is to count the number of even elements and number of odd elements of an array.

Example:

INPUT:
Enter the size of array: 5
Array[5] = {1, 2, 3, 4, 5}
OUTPUT:
Number of odd elements: 3
Number of even elements: 2

The steps required to count number of even element and odd elements are as follows:

  1. Set odd_count  = 0 and even_count = 0.
  2. Start scanning the array element. For each array element, check if it even or odd.

If the array element is even number then, increment even_count by

even_count = even_count + 1.

If the array element is odd number then, increment odd_count by

odd_count = odd_count + 1.

3. Print the odd_count and even_count

C++ Program to count number of even element and odd elements in an array are as follows:

/* Program to count even and odd elements in an array */  
#include<bits/stdc++.h>  
using namespace std;  
int main()  
{  
    int n;  
      
    /* Scanning size of the array */  
    cout<<"Enter the number of array elements: ";  
    cin>>n;  
      
    /* Creating an Array */  
    int arr[n];  
      
    /* Scanning the array elements */  
    cout<<"\nEnter the array elements: ";  
    for(int i = 0; i < n; i++)  
    cin>>arr[i];  
      
    int odd_count = 0;  
    int even_count = 0;  
      
    /* Count the even and odd elements */  
    for(int i = 0; i < n; i++)  
    {  
        if(arr[i] % 2 == 0)  
        even_count = even_count + 1;  
        else if(arr[i] % 2 != 0)  
        odd_count = odd_count + 1;  
    }  
      
    /* Printing the result */  
    cout<<"\nThe number of odd elements in an array are: "<<odd_count<<"\n";  
    cout<<"The number of even elements in an array are: "<<even_count<<"\n";  
}  

OUTPUT:
 
Enter the number of array elements: 5
Enter the array elements: 1 2 3 4 5
The number of odd elements in an array are: 3
The number of even elements in an array are: 2
 Time Complexity:
O(n), where n is the size of the array

Related Posts:

  1. Program to Reverse an Array.
  2. Program to find absolute difference between sum of odd index elements and even index elements.
  3. Program to find smallest and largest element of the array.
  4. Program to count occurrences of a particular element in an array.
  5. Program to search an element in an Array (Linear Search).
  6. Program to Rotate Array Elements by ‘D’ positions.
  7. Program to find most frequent element in an array.
  8. Program to find pair in an array with given sum.
  9. Program to find first repeating element of an array.
  10. Program to merge two sorted arrays.
  11. Program to find missing number in an array.
  12. Program to sort if array is sorted.
  13. Program to print Alternate Elements of an Array.
  14. Program to swap kth element from beginning to kth element from end in an Array.
  15. Program to print all possible subarrays of the given array.
  16. Program to print kth smallest and kth largest element of an Array.
  17. Program to find equilibrium index of an Array.
  18. Program to find majority element of an Array.
  19. Program to find mean of the Array.
  20. Program to sort an Array of 0s and 1s.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *