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 even and odd numbers in an array first user first enters the size of the array.

Then using for loop array elements are stored in an array “arrNum”. To distinguish between odd number and even number modulus operator is used.

Also read – java program to print even and odd numbers

Using for loop and if condition odd number and even number is determined. Let’s learn java program to print odd and even numbers in an array using for loop,

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 in java
        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 in java
        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 enters size of the array using scanner class.

Then user enters all the numbers or elements of an array. Now to sort and check even and odd numbers from an array use for loop and if condition.

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

Lastly print even & odd elements of an array in 2 seperate arrays on console.

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 of 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 of 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 of 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 of 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 of an array: 10
Please enter all the elements of an array:
1
2
3
4
5
6
7
8
9
10
Odd elements of an array: 1,3,5,7,9
Even elements of an array: 2,4,6,8,10


Count even numbers in an array java

To count even numbers in an array java first user enters the size of the array using scanner class.

Then array elements or numbers is entered. To find given number is even or odd number is divided by 2.

Then using modulus operator if remainder is zero then it is even number else odd number.

Here’s the java program to count even numbers in an array in java,

import java.util.Scanner;

public class CountEvenNumbersInArray 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      int arrSize, a, countEven = 0;
      System.out.print("Please enter number of elements of an array: ");
      arrSize = sc.nextInt();
      int[] arrNumbers = new int[arrSize];
      System.out.print("Please enter " + arrSize + " elements of an array: ");
      for(a = 0; a < arrSize; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }   
      System.out.print("Count even numbers in an array java: "); 
      for(a = 0; a < arrSize; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            System.out.print(arrNumbers[a] + " ");
            countEven++;
         }
      }       
      System.out.println("\nTotal number of even numbers in an array java: " + countEven);
      sc.close();
   }
}


Output:

Please enter number of elements of an array: 10
Please enter 10 elements of an array: 11
12
13
14
15
16
17
18
19
20
Count even numbers in an array java: 12 14 16 18 20
Total number of even numbers in an array java: 5


Java program to count even and odd numbers in an array

In java code to count even and odd numbers of an array first user enters the size of single dimensional array.

Then user enters elements of an array. Lastly using for loop even and odd numbers in an array is counted.

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

import java.util.Scanner;

public class CountEvenOddNumbers 
{
   public static void main(String[] args) 
   {
      int evenNumber = 0, oddNumber = 0;
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter the array size of an array: \n");    
      int arrSize = sc.nextInt();
      System.out.print("Please enter the elements of an array: \n");  
      int[] arrNumbers = new int[arrSize];    
      for(int a = 0; a < arrNumbers.length; a++)
      {
         arrNumbers[a] = sc.nextInt();
      }
      for(int a = 0; a < arrSize; a++)
      {
         if(arrNumbers[a] % 2 == 0)
         {
            evenNumber++;
         }
         else
         {
            oddNumber++;
         }
      }
      System.out.println("Count even numbers in an array: " + evenNumber);
      System.out.println("Count odd numbers in an array: " + oddNumber);
      sc.close();
   }
}


Output:

Please enter the array size of an array:
10
Please enter the elements of an array:
21
22
23
24
25
26
27
28
29
30
Count even numbers in an array: 5
Count odd numbers in an array: 5


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