Java program to print odd and even numbers in an array

Let’s learn java program to print odd and even numbers in an array.

Java program to print odd and even numbers in an array

In java program to print odd and even numbers in an array first user enters number of elements in an array using nextInt() method of Scanner class.

Then using for loop array elements are stored in an integer array “arrNum”.

Also read – java program to print even and odd numbers

Then using for loop and if condition odd or even is determined from given integer array.

Let’s learn java program to print odd and even numbers in an array.

import java.util.Scanner;
public class EvenOddArray
{
    public static void main(String[] args)
    {
        int numbers;
        Scanner sc = new Scanner(System.in);
        System.out.print("Please enter elements in array : ");
        numbers = sc.nextInt();
        int[] arrNum = new int[numbers];
        System.out.println("Enter " + numbers + " elements : ");
        for(int a = 0; a < numbers; a++) 
        {
            arrNum[a] = sc.nextInt();
        }
        // print odd numbers
        System.out.print("Odd numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 != 0)
            {
                System.out.print(arrNum[a] + " ");
            }
        }
        System.out.println("");
        // print even numbers
        System.out.print("Even numbers : ");
        for(int a = 0 ; a < numbers ; a++)
        {
            if(arrNum[a] % 2 == 0)
            {
                System.out.print(arrNum[a] + " ");
            }
        }
        sc.close();
    }
}


Output:

java program to print odd and even numbers in an array

Java program to put even & odd elements of an array in 2 separate arrays

In java program to put even & odd elements of an array in 2 separate arrays first user is allowed to enter size and elements of one dimensional array using nextInt() method Scanner class.

Now to put even & odd elements of an array in 2 separate arrays use for loop and if condition.

Also read – write a program to print even and odd numbers in java using for loop

Lastly print the seperated even & odd elements of an array in 2 seperate arrays.

Here’s the java program to put even & odd elements of an array in 2 separate arrays.

import java.util.Scanner;
public class EvenOdd2SeperateArrays
{
   public static void main(String[] args) 
   {
      int number, b = 0, c = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter number of elements in an array: ");
      number = sc.nextInt();
      int[] arrNumber = new int[number];
      int[] oddNumber = new int[number];
      int[] evenNumber = new int[number];
      System.out.println("Please enter all the elements in an array: ");
      for(int a = 0; a < number; a++)
      {
         arrNumber[a] = sc.nextInt();
      }
      for(int a = 0; a < number; a++)
      {
         if(arrNumber[a] % 2 != 0)
         {
            oddNumber[b] = arrNumber[a];
            b++;
         }
         else
         {
            evenNumber[c] = arrNumber[a];
            c++;
         }
      }
      System.out.print("Odd elements in an array: ");
      if(b > 1)
      {
         for(int a = 0; a < (b - 1); a++)
         {
            System.out.print(oddNumber[a] + ",");
         }
         System.out.print(oddNumber[b - 1]);
      }
      else
      {
         System.out.println("number not entered");
      }
      System.out.println("");
      System.out.print("Even elements in an array: ");
      if(c > 1)
      {
         for(int a = 0; a < (c - 1); a++)
         {
            System.out.print(evenNumber[a] + ",");
         }
         System.out.print(evenNumber[c - 1]);
      }
      else
      {
         System.out.println("number not entered");
      }
      sc.close();
   }
}


Output:

Please enter number of elements in an array: 10
Please enter all the elements in an array:
1
2
3
4
5
6
7
8
9
10
Odd elements in an array: 1,3,5,7,9
Even elements in an array: 2,4,6,8,10


count even and odd numbers in an array

To count even and odd numbers in an array first user is allowed to enter size and elements of one dimensional array using nextInt() method of Scanner class.

To find given number is even or odd, number is divided by 2. If remainder is 0 then it is even number else odd number.

Also read – continue statement in java

Finally number of even and odd numbers in the given array is counted and printed on console.

Here’s java program to count even and odd numbers in an array.

import java.util.Scanner;
public class CountEvenOddNumbersInArray
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int arrSize, a, countEven = 0, countOdd = 0;
      System.out.println("Please enter number of elements of an array: ");
      arrSize = sc.nextInt();
      int[] arrNumbers = new int[arrSize];
      System.out.println("Please enter " + arrSize + " elements of an array: ");
      for(a = 0; a < arrSize; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(a = 0; a < arrNumbers.length; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            countEven++;
         }
         else
         {
            countOdd++;
         }
      }
      System.out.println("Total number of even numbers in an array java: " + countEven);
      System.out.println("Total number of odd numbers in an array java: " + countOdd);
      sc.close();
   }
}


Output:

Please enter number of elements of an array: 5
Please enter 5 elements of an array: 1
2
3
4
5
Total number of even numbers in an array java: 2
Total number of odd numbers in an array java: 3


java program to print odd and even numbers between 1 and 100

Here’s the java program to print odd and even numbers between 1 and 100.

public class EvenOddBetween1And100
{
   public static void main(String[] args)
   {
      System.out.println("Even numbers between 1 and 100: ");
      for(int a = 1; a <= 100; a++)
      {
         if(a % 2 == 0)
         {
            System.out.print(a + " ");
         }
      }
      System.out.println("\nOdd numbers between 1 and 100: ");
      for(int a = 1; a <= 100; a++)
      {
         if(a % 2 != 0)
         {
            System.out.print(a + " ");
         }
      }
   }
}


Output:

Even numbers between 1 and 100:
2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100
Odd numbers between 1 and 100:
1 3 5 7 9 11 13 15 17 19 21 23 25 27 29 31 33 35 37 39 41 43 45 47 49 51 53 55 57 59 61 63 65 67 69 71 73 75 77 79 81 83 85 87 89 91 93 95 97 99


java program to find sum of even and odd numbers in an array

Similar to above java programs here’s java program to find sum of even and odd numbers in an array.

import java.util.Scanner;
public class FindSumOf
{
   public static void main(String[] args)
   {
      int number, sumEven = 0, sumOdd = 0;
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter number of elements in an array: ");
      number = sc.nextInt();
      int[] arrNumbers = new int[number];
      System.out.println("Please enter elements of the array: ");
      for(int a = 0; a < number; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(int a = 0; a < number; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            sumEven = sumEven + arrNumbers[a];
         }
         else
         {
            sumOdd = sumOdd + arrNumbers[a];
         }
      }
      System.out.println("Sum of even numbers in an array: " + sumEven);
      System.out.println("Sum of odd numbers in an array : " + sumOdd);
      sc.close();
   }
}


Output:

Please enter number of elements in an array: 8
Please enter elements of the array:
1
2
3
4
5
6
7
8
Sum of even numbers in an array: 20
Sum of odd numbers in an array : 16