In the previous article, we have seen Java Program to Separate Odd and Even Integers in Separate Arrays
In this article we are going to see how to separate odd and even integers in separate arrays in Java.
Java Program to Separate Even and Odd Integers in an Array of Integers
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 separate odd and even integers in separate arrays.
Method-1: Java Program to Separate Even and Odd Integers in an Array of Integers By Static Initialization of Array Elements
Approach:
- Create an array with elements, and two blank arrays of same size.
- Display the array elements to the user.
- Pass both the arrays into an user function
segregate()
that segregates the elements by traversing through the array and storing odd and even elements at their respective arrays. - Print both odd and even arrays.
Program:
import java.util.*; public class Main { public static void main(String args[]) { // Creating the array int arr[] = {12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68,10,20,30}; int odd[] = new int[arr.length], even[] = new int[arr.length]; // Prints the array elements System.out.println("The array elements are "+ Arrays.toString(arr)); segregate(arr,odd,even); } // Function that segregates the array into two arrays static void segregate(int arr[], int odd[], int even[]) { int oddCounter = 0, evenCounter = 0; // Segregating the array into two smaall arrays odd and even for(int i:arr) { if(i%2==0) odd[oddCounter++]=i; else even[evenCounter++]=i; } System.out.print("\nThe odd array elements are : "); // Prints odd element array for(int i=0;i<oddCounter;i++) System.out.print(odd[i]+" "); System.out.print("\nThe even array elements are : "); // Prints even element array for(int i=0;i<evenCounter;i++) System.out.print(even[i]+" "); } }
Output: The array elements are [12, 22, 34, 22, 54, 6, 52, 8, 9, 34, 54, 68, 10, 20, 30] The odd array elements are : 12 22 34 22 54 6 52 8 34 54 68 10 20 30 The even array elements are : 9
Method-2: Java Program Separate the Number of Even and Odd Integers in an Array of Integers By Dynamic Initialization of Array Elements
Approach:
- Create an array by taking array elements as input, and two blank arrays of same size.
- Display the array elements to the user.
- Pass both the arrays into an user function
segregate()
that segregates the elements by traversing through the array and storing odd and even elements at their respective arrays. - Print both odd and even arrays.
Program:
import java.util.*; public class Main { public static void main(String args[]) { Scanner scan = new Scanner(System.in); // Taking size as input from the user System.out.println("Enter the array size :"); int size = scan.nextInt(); // Creating the array int arr[] = new int[size]; // Entering the array elements System.out.println("Enter array elements : "); for(int i=0;i<size;i++) { arr[i] = scan.nextInt(); } int odd[] = new int[arr.length], even[] = new int[arr.length]; // Prints the array elements System.out.println("The array elements are "+ Arrays.toString(arr)); segregate(arr,odd,even); } // Function that segregates the array into two arrays static void segregate(int arr[], int odd[], int even[]) { int oddCounter = 0, evenCounter = 0; // Segregating the array into two smaall arrays odd and even for(int i:arr) { if(i%2==0) even[evenCounter++]=i; else odd[oddCounter++]=i; } System.out.print("\nThe odd array elements are : "); // Prints odd element array for(int i=0;i<oddCounter;i++) System.out.print(odd[i]+" "); System.out.print("\nThe even array elements are : "); // Prints even element array for(int i=0;i<evenCounter;i++) System.out.print(even[i]+" "); } }
Outpu: Enter the array size : 8 Enter array elements : 1 2 3 4 5 6 7 8 The array elements are [1, 2, 3, 4, 5, 6, 7, 8] The odd array elements are : 2 4 6 8 The even array elements are : 1 3 5 7
Are you a job seeker and trying to find simple java programs for Interview? This would be the right choice for you, just tap on the link and start preparing the java programs covered to crack the interview.
Related Java Programs: