Count Even and Odd numbers in an Array

Count Even and Odd numbers in an 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 Even and Odd numbers in an Array 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.
  3. If the array element is even number then, increment even_count by: even_count = even_count + 1.
  4. If the array element is odd number then, increment odd_count by: odd_count = odd_count + 1.
  5. Print the odd_count and even_count

C++ Program to Count Even and Odd numbers in an Array are as follows:

/* C++ Program to Count Even and Odd numbers 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 find absolute difference between sum of odd index elements and even index elements.
  2. Program to find smallest and largest element of the array.
  3. Program to count occurrences of a particular element in an array.
  4. Program to search an element in an Array (Linear Search).
  5. Program to Rotate Array Elements by ‘D’ positions.
  6. Program to find most frequent element in an array.
  7. Program to find pair in an array with given sum.
  8. Program to find first repeating element of an array.
  9. Program to merge two sorted arrays.
  10. Program to find missing number in an array.
  11. Program to sort if array is sorted.
  12. Program to Check whether given String is Palindrome or not.
  13. Program to convert lower-case alphabet to upper-case alphabet and vice-versa.
  14. Program to count total number of words in a string.
  15. Program to find smallest and largest word of the String.
  16. Program to find Most Frequent Character of the String.
  17. Program to Remove all the blank spaces from the String.
  18. Program to check if String is isogram or not.
  19. Program to Reverse Each word of the String.
  20. Program to Print All the Substring of the Given String.

You may also like...

Leave a Reply

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