Java Program to Separate Odd and Even Numbers from an Array

This is a Java Program to Put Even & Odd Elements of an Array in 2 Separate Arrays.

Enter size of array and then enter all the elements of that array. Now with the help of for loop and if condition we check whether its odd or not and hence print it accordingly.

Here is the source code of the Java Program to Put Even & Odd Elements of an Array in 2 Separate Arrays. The Java program is successfully compiled and run on a Windows system. The program output is also shown below.

  1. import java.util.Scanner;
  2. public class Even_Odd
  3. {
  4.     public static void main(String[] args) 
  5.     {
  6.         int n, j = 0, k = 0;
  7.         Scanner s = new Scanner(System.in);
  8.         System.out.print("Enter no. of elements you want in array:");
  9.         n = s.nextInt();
  10.         int a[] = new int[n];
  11.         int odd[] = new int[n];
  12.         int even[] = new int[n];
  13.         System.out.println("Enter all the elements:");
  14.         for(int i = 0; i < n; i++)
  15.         {
  16.             a[i] = s.nextInt();
  17.         }
  18.         for(int i = 0; i < n; i++)
  19.         {
  20.             if(a[i] % 2 != 0)
  21.             {
  22.                 odd[j] = a[i];
  23.                 j++;
  24.             }
  25.             else
  26.             {
  27.                 even[k] = a[i];
  28.                 k++;
  29.             }
  30.         }
  31.         System.out.print("Odd:");
  32.         if(j > 1)
  33.         {
  34.             for(int i = 0;i < (j-1); i++)
  35.             {
  36.                 System.out.print(odd[i]+",");
  37.             }
  38.             System.out.print(odd[j-1]);
  39.         }
  40.         else
  41.         {
  42.             System.out.println("No number");
  43.         }
  44.         System.out.println("");
  45.         System.out.print("Even:");
  46.         if(k > 1)
  47.         {
  48.             for(int i = 0; i < (k-1); i++)
  49.             {
  50.                 System.out.print(even[i]+",");
  51.             }
  52.         System.out.print(even[k-1]);
  53.         }
  54.         else
  55.         {
  56.             System.out.println("No number");
  57.         }
  58.     }
  59. }

Output:

$ javac Even_Odd.java
$ java Even_Odd
 
Enter no. of elements you want in array:8
Enter all the elements:
1
2
3
4
5
6
7
8
Odd:1,3,5,7
Even:2,4,6,8

Sanfoundry Global Education & Learning Series – 1000 Java Programs.

advertisement
advertisement

Here’s the list of Best Books in Java Programming, Data Structures and Algorithms.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.