For the given array of integers, count even and odd elements.
Examples:
Input : int arr[5] = {2, 3, 4, 5, 6}
Output : Number of even elements = 3
Number of odd elements = 2
Input : int arr[5] = {22, 32, 42, 52, 62}
Output : Number of even elements = 5
Number of odd elements = 0
Simple approach: A number is said to be Even if it is completely divisible by 2. A number is said to be Odd if it is leaves 1 as remainder on dividing by 2.
Other approach: We can also check if a number is odd by doing AND of 1 and that digit, if the result comes out to be 1 then the number is odd otherwise even.
Here, we will check if a number is odd , then we will increment the odd
counter otherwise we will increment the even counter.
C++
// CPP program to count number of even // and odd elements in an array #include<iostream> using namespace std; void CountingEvenOdd(int arr[], int arr_size) { int even_count = 0; int odd_count = 0; // loop to read all the values in the array for(int i = 0 ; i < arr_size ; i++) { // checking if a number is completely // divisible by 2 if (arr[i] & 1 == 1) odd_count ++ ; else even_count ++ ; } cout << "Number of even elements = " << even_count << "\nNumber of odd elements = " << odd_count ; } // Driver Code int main() { int arr[] = {2, 3, 4, 5, 6}; int n = sizeof(arr) / sizeof(arr[0]); CouningEvenOdd(arr, n); } |
Java
// java program to count number of even // and odd elements in an array import java.io.*; class GFG { static void CountingEvenOdd(int arr[], int arr_size) { int even_count = 0; int odd_count = 0; // loop to read all the values in // the array for(int i = 0 ; i < arr_size ; i++) { // checking if a number is // completely divisible by 2 if ((arr[i] & 1) == 1) odd_count ++ ; else even_count ++ ; } System.out.println( "Number of even" + " elements = " + even_count + " Number of odd elements = " + odd_count) ; } // Driver Code public static void main (String[] args) { int arr[] = {2, 3, 4, 5, 6}; int n = arr.length; CountingEvenOdd(arr, n); } } // This code is Contributed by anuj_67. |
Python3
# Python3 program to count number of # even and odd elements in an array def CountingEvenOdd(arr, arr_size): even_count = 0 odd_count = 0 # loop to read all the values # in the array for i in range(arr_size): # checking if a number is # completely divisible by 2 if (arr[i] & 1 == 1): odd_count += 1 else: even_count += 1 print("Number of even elements = ", even_count) print("Number of odd elements = " , odd_count) # Driver Code arr = [2, 3, 4, 5, 6] n = len(arr) CountingEvenOdd(arr, n) # This code is contributed by sahishelangia |
C#
// C# program to count number of even // and odd elements in an array using System; class GFG { static void CountingEvenOdd(int []arr, int arr_size) { int even_count = 0; int odd_count = 0; // loop to read all the values in // the array for(int i = 0 ; i < arr_size ; i++) { // checking if a number is // completely divisible by 2 if ((arr[i] & 1) == 1) odd_count ++ ; else even_count ++ ; } Console.WriteLine( "Number of even" + " elements = " + even_count + " Number of odd elements = " + odd_count) ; } // Driver Code public static void Main () { int []arr = {2, 3, 4, 5, 6}; int n = arr.Length; CountingEvenOdd(arr, n); } } // This code is Contributed by anuj_67. |
PHP
<?php // PHP program to count number of even // and odd elements in an array function CountingEvenOdd( $arr, $arr_size) { $even_count = 0; $odd_count = 0; // loop to read all the values in // the array for( $i = 0 ; $i < $arr_size ; $i++) { // checking if a number is // completely divisible by 2 if ($arr[$i] & 1 == 1) $odd_count ++ ; else $even_count ++ ; } echo "Number of even elements = " , $even_count," Number of odd " , "elements = " ,$odd_count ; } // Driver Code $arr = array(2, 3, 4, 5, 6); $n = count($arr); CountingEvenOdd($arr, $n); // This code is Contributed by anuj_67. ?> |
Output:
Number of even elements = 3 Number of odd elements = 2
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Even numbers at even index and odd numbers at odd index
- Count number of even and odd length elements in an Array
- Rearrange array such that even index elements are smaller and odd index elements are greater
- Increment odd positioned elements by 1 and decrement even positioned elements by 1 in an Array
- Find Kth element in an array containing odd elements first and then even elements
- Split an Array to maximize subarrays having equal count of odd and even elements for a cost not exceeding K
- Count of even and odd set bit Array elements after XOR with K for Q queries
- Construct an Array of size N in which sum of odd elements is equal to sum of even elements
- Replace the odd positioned elements with their cubes and even positioned elements with their squares
- Count subarrays with same even and odd elements
- Count of odd and even parity elements in subarray using MO's algorithm
- Query to count odd and even parity elements in subarray after XOR with K
- Count subarrays having sum of elements at even and odd positions equal
- Generate an Array in which count of even and odd sum sub-arrays are E and O respectively
- Rearrange array such that all even-indexed elements in the Array is even
- Program to print Sum of even and odd elements in an array
- Program to print product of even and odd indexed elements in an Array
- Absolute Difference of even and odd indexed elements in an Array
- Modify given array to make sum of odd and even indexed elements same
- Count of odd and even sum pairs in an array
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

