ISC Practicals and Theory - Odd-Even Segregation (OOP)
Updated 21 Oct 2026
Odd-even segregation rearranges an integer array so that all even numbers come first and all odd numbers follow. This version is a class: the size goes to a parameterised constructor, isEven() decides the type of a number, copyGroup() copies one group into a second array and segregate() calls it twice. It reads 1 to 20 integers.
Definition
An integer is even if x % 2 == 0 and odd otherwise. Zero is even. In Java -3 % 2 is -1, so oddness must not be tested as % 2 == 1; this program tests evenness with == 0, which is correct for negative numbers too. Because a second array is used, the order inside each group is the entered order (stable). For 3 -2 6 -7 0 the evens in order are -2 6 0 and the odds are 3 -7, so the result is -2 6 0 3 -7.
Algorithm
- In
main, readsize. Ifsize < 1orsize > 20, print an error message and stop. - Create
objwithnew OddEvenSegregationOOP(size). The constructor setsn = size, createsarrandresof sizenand setsk = 0. readElements(sc)readsnintegers intoarr.segregate()setsk = 0, callscopyGroup(true)for the evens and thencopyGroup(false)for the odds.copyGroup(wantEven)scansarrfrom left to right. IfisEven(arr[i]) == wantEven, it setsres[k] = arr[i]and increasesk.isEven(x)returnsx % 2 == 0.
display()printsres[0]tores[n - 1].
Program
The class OddEvenSegregationOOP has the data members n, arr, res and k.
import java.util.Scanner;
/**
* Class OddEvenSegregationOOP rearranges an integer array so that all even numbers
* come first and all odd numbers come after them.
* A second array is used, so the order inside each group stays as entered (stable).
* The size is passed to the parameterised constructor.
*/
public class OddEvenSegregationOOP
{
int n; // number of elements in the array (1 to 20)
int[] arr; // original array as entered
int[] res; // rearranged array: evens first, then odds
int k; // next free position in res while it is being filled
/**
* Parameterised constructor: sets the size and creates both arrays.
* @param size the number of elements, already checked to be from 1 to 20
*/
OddEvenSegregationOOP(int size)
{
n = size;
arr = new int[n];
res = new int[n];
k = 0; // res is empty at the start
}
/**
* Reads the n elements into arr.
* @param sc the Scanner used to read input
*/
void readElements(Scanner sc)
{
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
}
/**
* Checks whether a number is even. 0 is even.
* For a negative number x % 2 is 0 or -1, so evenness is tested as == 0.
* @param x the number to test
* @return true if x is even, false if it is odd
*/
boolean isEven(int x)
{
return x % 2 == 0;
}
/**
* Copies into res every element of arr whose evenness matches the request.
* @param wantEven true to copy the even elements, false to copy the odd ones
*/
void copyGroup(boolean wantEven)
{
for (int i = 0; i < n; i++)
{
if (isEven(arr[i]) == wantEven)
{
res[k] = arr[i];
k++;
}
}
}
/**
* Fills res with the even numbers first and then the odd numbers,
* keeping the entered order inside each group.
*/
void segregate()
{
k = 0;
copyGroup(true); // evens first
copyGroup(false); // then odds
}
/**
* Displays the rearranged array.
*/
void display()
{
System.out.println("Array with even numbers first, then odd numbers:");
for (int i = 0; i < n; i++)
{
System.out.print(res[i] + " ");
}
System.out.println();
}
/**
* Reads and checks the size, creates the object and calls the methods.
* @param args command line arguments (not used)
*/
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements (1 to 20): ");
int size = sc.nextInt();
if (size < 1 || size > 20)
{
System.out.println("Invalid input. Size must be from 1 to 20.");
return;
}
OddEvenSegregationOOP obj = new OddEvenSegregationOOP(size);
obj.readElements(sc);
obj.segregate();
obj.display();
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
n | int | Data member: number of elements in the array |
arr | int[] | Data member: original array as entered |
res | int[] | Data member: rearranged array with even numbers first, then odd numbers |
k | int | Data member: next free position in res while it is being filled |
size | int | Size read in main; also the parameter of the constructor |
sc | Scanner | Scanner object in main to read input; also the parameter of readElements() |
x | int | Parameter of isEven(): the number to test |
wantEven | boolean | Parameter of copyGroup(): true to copy the even elements, false to copy the odd ones |
i | int | Loop counter for reading arr, for scanning it in copyGroup() and for printing res |
obj | OddEvenSegregationOOP | Object created in main to call the methods |
args | String[] | Parameter: command line arguments of main |
Sample input and output
Sample 1
Enter the number of elements (1 to 20): 5
Enter the elements:
3 -2 6 -7 0
Array with even numbers first, then odd numbers:
-2 6 0 3 -7 Sample 2
Enter the number of elements (1 to 20): 9
Enter the elements:
5 -3 8 0 -4 7 2 11 -6
Array with even numbers first, then odd numbers:
8 0 -4 2 -6 5 -3 7 11 Sample 3
Enter the number of elements (1 to 20): 3
Enter the elements:
9 -1 15
Array with even numbers first, then odd numbers:
9 -1 15 Sample 4
Enter the number of elements (1 to 20): 0
Invalid input. Size must be from 1 to 20.Dry run
Input: size = 5, arr = [3, -2, 6, -7, 0], k = 0. This is the first sample run.
| Call | i | arr[i] | isEven(arr[i]) | Copied? | res so far | k |
|---|---|---|---|---|---|---|
copyGroup(true) | 0 | 3 | false | No | [] | 0 |
| 1 | -2 | true | Yes | [-2] | 1 | |
| 2 | 6 | true | Yes | [-2, 6] | 2 | |
| 3 | -7 | false | No | [-2, 6] | 2 | |
| 4 | 0 | true | Yes | [-2, 6, 0] | 3 | |
copyGroup(false) | 0 | 3 | false | Yes | [-2, 6, 0, 3] | 4 |
| 1 | -2 | true | No | unchanged | 4 | |
| 2 | 6 | true | No | unchanged | 4 | |
| 3 | -7 | false | Yes | [-2, 6, 0, 3, -7] | 5 | |
| 4 | 0 | true | No | unchanged | 5 |
The final array is -2 6 0 3 -7, which matches the first sample run.
Variations
- Positive and negative "Rearrange an array so that all negative numbers come before the positive numbers."
- Zeros and ones "Rearrange an array of 0s and 1s so that all 0s come first."
- Odd first "Rearrange an array so that all odd numbers come first and the even numbers last."
- Sorted groups "Segregate the even and odd numbers and sort each group in ascending order."