ISC Practicals and Theory - Odd-Even Segregation
Updated 21 Oct 2026
Odd-even segregation rearranges an integer array so that all the even numbers come first and all the odd numbers come after them. This program, written in the main method alone, reads 1 to 20 integers, copies the even numbers into a second array and then the odd numbers, and prints the result.
Definition
An integer is even if it leaves remainder 0 when divided by 2, and odd otherwise. Segregation puts every even number before every odd number.
- Zero is even.
- Negative numbers: in Java the remainder takes the sign of the dividend, so
-3 % 2is-1, not1. A number is therefore tested as odd with% 2 != 0and as even with% 2 == 0; testing% 2 == 1would wrongly treat-3as even. - Order: this program uses a second array, so the numbers inside each group keep the order in which they were entered (a stable arrangement).
For 5 -3 8 0 -4 7 2 11 -6 the evens in order are 8 0 -4 2 -6 and the odds in order are 5 -3 7 11, so the result is 8 0 -4 2 -6 5 -3 7 11.
Size constraint: the size must be from 1 to 20.
Algorithm
- Read the number of elements
n. If it is not from 1 to 20, print an error message and stop. - Read the
nelements into the arrayarrand create the arrayresof the same size. Setk = 0, the next free position inres. - First pass: for every
i, ifarr[i] % 2 == 0, copyarr[i]tores[k]and add 1 tok. - Second pass: for every
i, ifarr[i] % 2 != 0, copyarr[i]tores[k]and add 1 tok. A negative odd number gives a remainder of -1 (for example-3 % 2is -1), so the test is!= 0and not== 1. - Print
res. The even numbers come first and the odd numbers after them, each group in its original order.
Program
Two for loops in main copy the even numbers and then the odd numbers into res; a third loop prints it.
import java.util.Scanner;
public class OddEvenSegregation
{
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.print("Enter the number of elements (1 to 20): ");
int n = sc.nextInt(); // number of elements entered by the user (1 to 20)
if (n < 1 || n > 20) // size outside the allowed range
{
System.out.println("Invalid input. Size must be from 1 to 20.");
}
else
{
int[] arr = new int[n]; // original array as entered
int[] res = new int[n]; // rearranged array: evens first, then odds
System.out.println("Enter the elements:");
for (int i = 0; i < n; i++)
{
arr[i] = sc.nextInt();
}
int k = 0; // next free position in res
for (int i = 0; i < n; i++) // first pass: copy the even numbers
{
if (arr[i] % 2 == 0)
{
res[k] = arr[i];
k++;
}
}
for (int i = 0; i < n; i++) // second pass: copy the odd numbers
{
if (arr[i] % 2 != 0) // -3 % 2 is -1, so do not test == 1
{
res[k] = arr[i];
k++;
}
}
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();
}
}
}Variable description table
| Variable | Data Type | Purpose |
|---|---|---|
args | String[] | Command line arguments of main; not used |
sc | Scanner | Reads the numbers typed by the user |
n | int | Number of elements entered by the user (1 to 20) |
arr | int[] | The original array as entered |
res | int[] | The rearranged array: evens first, then odds |
i | int | Loop counter over the elements |
k | int | Next free position in res |
Sample input and output
Sample 1
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 2
Enter the number of elements (1 to 20): 4
Enter the elements:
2 4 0 -8
Array with even numbers first, then odd numbers:
2 4 0 -8 Sample 3
Enter the number of elements (1 to 20): 1
Enter the elements:
7
Array with even numbers first, then odd numbers:
7 Sample 4
Enter the number of elements (1 to 20): 25
Invalid input. Size must be from 1 to 20.Dry run
Input: n = 9, arr = [5, -3, 8, 0, -4, 7, 2, 11, -6], k = 0.
First pass (even numbers):
i | arr[i] | arr[i] % 2 | Even? | res so far | k |
|---|---|---|---|---|---|
| 0 | 5 | 1 | No | [] | 0 |
| 1 | -3 | -1 | No | [] | 0 |
| 2 | 8 | 0 | Yes | [8] | 1 |
| 3 | 0 | 0 | Yes | [8, 0] | 2 |
| 4 | -4 | 0 | Yes | [8, 0, -4] | 3 |
| 5 | 7 | 1 | No | [8, 0, -4] | 3 |
| 6 | 2 | 0 | Yes | [8, 0, -4, 2] | 4 |
| 7 | 11 | 1 | No | [8, 0, -4, 2] | 4 |
| 8 | -6 | 0 | Yes | [8, 0, -4, 2, -6] | 5 |
Second pass (odd numbers):
i | arr[i] | arr[i] % 2 | Odd? | res so far | k |
|---|---|---|---|---|---|
| 0 | 5 | 1 | Yes | [8, 0, -4, 2, -6, 5] | 6 |
| 1 | -3 | -1 | Yes | [8, 0, -4, 2, -6, 5, -3] | 7 |
| 2 | 8 | 0 | No | unchanged | 7 |
| 3 | 0 | 0 | No | unchanged | 7 |
| 4 | -4 | 0 | No | unchanged | 7 |
| 5 | 7 | 1 | Yes | [8, 0, -4, 2, -6, 5, -3, 7] | 8 |
| 6 | 2 | 0 | No | unchanged | 8 |
| 7 | 11 | 1 | Yes | [8, 0, -4, 2, -6, 5, -3, 7, 11] | 9 |
| 8 | -6 | 0 | No | unchanged | 9 |
The final array is 8 0 -4 2 -6 5 -3 7 11, 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."