Prashnikaप्रश्निका
Code
About
‹ All Computer Science notes

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

  1. In main, read size. If size < 1 or size > 20, print an error message and stop.
  2. Create obj with new OddEvenSegregationOOP(size). The constructor sets n = size, creates arr and res of size n and sets k = 0.
  3. readElements(sc) reads n integers into arr.
  4. segregate() sets k = 0, calls copyGroup(true) for the evens and then copyGroup(false) for the odds.
    • copyGroup(wantEven) scans arr from left to right. If isEven(arr[i]) == wantEven, it sets res[k] = arr[i] and increases k.
    • isEven(x) returns x % 2 == 0.
  5. display() prints res[0] to res[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

VariableData TypePurpose
nintData member: number of elements in the array
arrint[]Data member: original array as entered
resint[]Data member: rearranged array with even numbers first, then odd numbers
kintData member: next free position in res while it is being filled
sizeintSize read in main; also the parameter of the constructor
scScannerScanner object in main to read input; also the parameter of readElements()
xintParameter of isEven(): the number to test
wantEvenbooleanParameter of copyGroup(): true to copy the even elements, false to copy the odd ones
iintLoop counter for reading arr, for scanning it in copyGroup() and for printing res
objOddEvenSegregationOOPObject created in main to call the methods
argsString[]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.

One value per line

Dry run

Input: size = 5, arr = [3, -2, 6, -7, 0], k = 0. This is the first sample run.

Calliarr[i]isEven(arr[i])Copied?res so fark
copyGroup(true)03falseNo[]0
1-2trueYes[-2]1
26trueYes[-2, 6]2
3-7falseNo[-2, 6]2
40trueYes[-2, 6, 0]3
copyGroup(false)03falseYes[-2, 6, 0, 3]4
1-2trueNounchanged4
26trueNounchanged4
3-7falseYes[-2, 6, 0, 3, -7]5
40trueNounchanged5

The final array is -2 6 0 3 -7, which matches the first sample run.

Variations

  1. Positive and negative "Rearrange an array so that all negative numbers come before the positive numbers."
  2. Zeros and ones "Rearrange an array of 0s and 1s so that all 0s come first."
  3. Odd first "Rearrange an array so that all odd numbers come first and the even numbers last."
  4. Sorted groups "Segregate the even and odd numbers and sort each group in ascending order."