How to Separate Numbers Into Odd & Even in Java


An integer number is even if it is exactly divisible by 2; it is odd otherwise. You can write a program in the Java programming language that tests each element in an input array for this property, and separates the elements into separate Collections -- one for even, one for odd.

  • Store the set of input integers as elements in an array, as in the following sample code:

    int[] inputNumbers = {43,543,245,2,56,567,8767,2,-32,41};

  • Declare two Java Collections to hold even numbers separately from odd ones, as in the following sample code:

    List<Integer> outputEven = new ArrayList<Integer>();

    List<Integer> outputOdd = new ArrayList<Integer>();

    The advantage of using Collections is that, in addition to providing many useful built-in methods, Collections only use memory for as many elements as they contain.

  • Separate the numbers into the two output Collections by iterating over the input, as in the following sample code:

    for (int i: inputNumbers) {

    if (i % 2 != 0) {


    } else {




    After executing this code, Collections outputEven and outputOdd will contain the even and odd numbers in the input, respectively.

Related Searches

Promoted By Zergnet



You May Also Like

Related Searches

Check It Out

Geek Vs Geek: Robot battles, hoverboard drag race, and more

Is DIY in your DNA? Become part of our maker community.
Submit Your Work!