Sunday, November 17, 2024
Google search engine
HomeData Modelling & AICount number of even and odd elements in an array

Count number of even and odd elements in an array

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

Solution: We can also check if a number is odd or even

  • By doing AND of 1 and that digit, if the result comes out to be 1 then the number is odd otherwise even.
  • By its divisibility by 2. A number is said to be odd if it is not divisible by 2, otherwise its even.

Here, we will check if a number is odd, then we will increment the odd counter otherwise we will increment the even counter. 

Below is the implementation of the above approach:

C




// C program to count number of even
// and odd elements in an array
#include <stdio.h>
  
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++;
    }
  
    printf("Number of even elements = %d \nNumber of odd "
           "elements = %d",
           even_count, odd_count);
}
  
// Driver Code
int main()
{
    int arr[] = { 2, 3, 4, 5, 6 };
    int n = sizeof(arr) / sizeof(arr[0]);
  
    // Function Call
    CountingEvenOdd(arr, n);
}


C++




// C++ 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]);
        
      // Function Call
    CountingEvenOdd(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;
            
          // Function Call
        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)
  
# Function Call
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;
        
          // Function Call
        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);
  
    // Function Call
    CountingEvenOdd($arr, $n);
  
// This code is Contributed by anuj_67.
?>


Javascript




<script>
  
// Javascript program to count number of even
// and odd elements in an array
  
function CountingEvenOdd(arr, arr_size)
{
    let even_count = 0;
    let odd_count = 0;
  
    // loop to read all the values in the array
    for (let 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++;
    }
  
    document.write("Number of even elements = " + even_count
        + "<br>" + "Number of odd elements = " + odd_count);
}
  
// Driver Code
  
    let arr = [ 2, 3, 4, 5, 6 ];
    let n = arr.length;
      
    // Function Call
    CountingEvenOdd(arr, n);
  
// This code is contributed by Mayank Tyagi
  
</script>


Output

Number of even elements = 3
Number of odd elements = 2

Time Complexity: O(n)

Auxiliary Space: O(1) because it is using constant space for variables

Feeling lost in the world of random DSA topics, wasting time without progress? It’s time for a change! Join our DSA course, where we’ll guide you on an exciting journey to master DSA efficiently and on schedule.
Ready to dive in? Explore our Free Demo Content and join our DSA course, trusted by over 100,000 neveropen!

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments