Java Program to Find the Number of Even and Odd Integers in an Array of Integers

In the previous article, we have seen Java Program to Separate Odd and Even Integers in Separate Arrays

In this article we are going to see how to find number of odd and even integers in separate arrays in Java.

Java Program to Find the Number of Even and Odd Integers in an Array of Integers

Array is a data structure which stores a fixed size sequential collection of values of single type. Where with every array elements/values memory location is associated. Each array elements have it’s own index where array index starts from 0.

In Array set of variables referenced by a single variable name and it’s array index position. It is also called as a container object which contains elements of similar type.

Declaration of an array:

dataType[] arrayName; (or)                              //Declaring an array
dataType []arrayName; (or)
dataType arr[];

Instantiation of an Array:

arrayName = new datatype[size];                    //Allocating memory to array

Combining both Statements in One:

dataType[] arrayName = new dataType[size] //Declaring and Instantiating array

Initialization of an Array:

arrayName[index-0]= arrayElement1             //Initializing the array

...

arrayName[index-s]= arrayElementS

Combining all Statements in One:

dataType arrayName[ ]={e1,e2,e3};               //declaration, instantiation and initialization

Let’s see different ways to find number of odd and even integers in separate arrays.

Method-1: Java Program to Find Number of Even and Odd Integers in an Array of Integers By Static Initialization of Array Elements

Approach:

  • Create an array with elements.
  • Display the array elements to the user.
  • Pass both the arrays into an user function segregate() that segregates the elements by traversing through the array and storing odd and even elements at their respective arrays.
  • Print count of even and odd elements.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        // Creating the array
        int arr[] = {2,3,4,5,6,7,8,9};
        // Prints the array elements
        System.out.println("The array elements are "+ Arrays.toString(arr));
        
        segregate(arr);

    }
    
    // Function that segregates the array into two arrays
    static void segregate(int arr[])
    {
        int oddCount = 0, evenCount = 0;
        // Segregating the array into two smaall arrays odd and even
        for(int i:arr)
        {
            if(i%2==0)
                evenCount+=1;
            else
                oddCount+=1;
        }

        System.out.print("\nThe number of odd elements are : "+oddCount);
        
        System.out.print("\nThe number of even elements are : "+evenCount);
    }
}
Output: 

The array elements are [2, 3, 4, 5, 6, 7, 8, 9]

The number of odd elements are : 4
The number of even elements are : 4

Method-2: Java Program Separate the Number of Even and Odd Integers in an Array of Integers By Dynamic Initialization of Array Elements

Approach:

  • Create an array by taking array elements as input.
  • Display the array elements to the user.
  • Pass both the arrays into an user function segregate() that segregates the elements by traversing through the array and storing odd and even elements at their respective arrays.
  • Print count of even and odd elements.

Program:

import java.util.*;

public class Main
{
    public static void main(String args[])
    {
        Scanner scan = new Scanner(System.in);
        
        // Taking size as input from the user
        System.out.println("Enter the array size :");
        int size = scan.nextInt();
        
        // Creating the array
        int arr[] = new int[size];
        
        // Entering the array elements
        System.out.println("Enter array elements : ");
        for(int i=0;i<size;i++)
        {
            arr[i] = scan.nextInt();
        }
        
        // Prints the array elements
        System.out.println("The array elements are "+ Arrays.toString(arr));
        
        segregate(arr);

    }
    
    // Function that segregates the array into two arrays
    static void segregate(int arr[])
    {
        int oddCount = 0, evenCount = 0;
        // Segregating the array into two smaall arrays odd and even
        for(int i:arr)
        {
            if(i%2==0)
                evenCount+=1;
            else
                oddCount+=1;
        }

        System.out.print("\nThe number of odd array elements are : "+oddCount);
        
        System.out.print("\nThe number of even array elements are : "+evenCount);

    }
}
Output:

Enter the array size :
Enter array elements : 
The array elements are [1, 2, 3, 4, 5]

The number of odd array elements are : 3
The number of even array elements are : 2

Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.

Related Java Programs: