Java Program to Find the Array Type Even Odd and Mixed

In the previous article, we have seen Java Program to Create an Array Containing the Square root of All the Elements in the Array

In this article we are going to see how to find the array type, whether even or odd or mixed type using Java programming language.

Java Program to Find the Array Type Even Odd and Mixed

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 check the array type whether it is even, odd or mixed type.

Method-1: Java Program to Find the Array Type Even Odd and Mixed By Static Initialization of Array Elements

Approach:

  • Initialize the array.
  • Initialize two variables oddCount = 0 and evenCount = 0.
  • Iterate over the array.
  • Check if the current element is even, increment evenCount by one else increment oddCount by one.
  • After the loop is exhausted, check if evenCount == array.length, print: Array is of even type.
  • Else if oddCount == array.length, print: Array is of odd type.
  • Else print: Array is of mixed type.

Program:

import java.util.Arrays;

public class Main 
{
    public static void main(String[] args) 
    {
        int[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        //method called to check array type
        checkArrayType(arr);
    }

    //checkArrayType() which will check the array type
    static void checkArrayType(int[] arr) 
    {
        int oddCount = 0;
        int evenCount = 0;
        for (int i : arr) 
        {
            if (i % 2 == 0) 
            {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        //if oddCount is equal to array length
        //means all elements in array are odd
        //so array is odd type
        if (oddCount == arr.length) 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of odd type");
        } 
        //if evenCount is equal to array length
        //means all elements in array are even
        //so array is even type
        else if (evenCount == arr.length) 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of even type");
        }
        //else it is a mixed type of even and odd
        else 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of mixed type");
        }
    }
}
Output:

Array [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] is of mixed type

Method-2: Java Program to Find the Array Type Even Odd and Mixed By Dynamic Initialization of Array Elements

Approach:

  • Create scanner class object.
  • Ask use length of the array.
  • Initialize the array with given size.
  • Ask the user for array elements.
  • Initialize two variables oddCount = 0 and evenCount = 0.
  • Iterate over the array.
  • Check if the current element is even, increment evenCount by one else increment oddCount by one.
  • After the loop is exhausted, check if evenCount == array.length, print: Array is of even type.
  • Else if oddCount == array.length, print: Array is of odd type.
  • Else print: Array is of mixed type.

Program:

import java.util.*;

public class Main 
{
    public static void main(String[] args) 
    {
        // create scanner class object
        Scanner sc = new Scanner(System.in);
        // take input from user for array size
        System.out.print("Enter the size of array: ");
        int n = sc.nextInt();
        // initialize array with size n
        int[] arr = new int[n];
        // take input from user for array elements
        System.out.print("Enter array elements: ");
        for (int i = 0; i < n; i++) {
            arr[i] = sc.nextInt();
        }

        //method called to check array type
        checkArrayType(arr);
    }

    //checkArrayType() which will check the array type
    static void checkArrayType(int[] arr) 
    {
        int oddCount = 0;
        int evenCount = 0;
        for (int i : arr) 
        {
            if (i % 2 == 0) 
            {
                evenCount++;
            } else {
                oddCount++;
            }
        }
        
        //if oddCount is equal to array length
        //means all elements in array are odd
        //so array is odd type
        if (oddCount == arr.length) 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of odd type");
        } 
        //if evenCount is equal to array length
        //means all elements in array are even
        //so array is even type
        else if (evenCount == arr.length) 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of even type");
        }
        //else it is a mixed type of even and odd
        else 
        {
            System.out.println("Array " + Arrays.toString(arr) +" is of mixed type");
        }
    }
}
Output:

Case-1

Enter the size of array: 6
Enter array elements: 16 88 90 24 44 68
Array [16, 88, 90, 24, 44, 68] is of even type


Case-2

Enter the size of array: 6
Enter array elements: 23 77 53 37 79 43
Array [23, 77, 53, 37, 79, 43] is of odd type


Case-3

Enter the size of array: 6
Enter array elements: 24 31 89 54 22 88 73
Array [24, 31, 89, 54, 22, 73] is of mixed type

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Related Java Programs: