PHP Program to separate the odd and even numbers in an array


February 3, 2022, Learn eTutorial
1805

How do we separate odd and even numbers in the array?

In this program, we are separating the odd and even numbers in the given array. Odd numbers are those numbers, whose remainder will be 1 when operated by 'Mod(%) 2' and the remainder will be 0 for an Even number. For example, if the entered number is 8 and when '8 % 2' is calculated, the result will be 0, so it is an even number. And if the entered number is 7 and when '7 % 2' is calculated the result will be 1, so it is an odd number. After finding the odd and even we assign them into different arrays.

separate the odd and even numbers in an array

How do we separate odd and even number in an array using PHP?

For this program to separate the odd and even elements in the array, we are taking the static values which are assigned to the array arr[]. After that, we assign two empty arrays oddArray[] and evenArray[] to store the odd and even numbers separately. Then we have to assign the size of the array into the variable size by using the built-in function count(). Then we can print the elements of the array arr[] using for loop. Now we have to assign the value 0 into the variables j and k. After that assign the value 0 into the variable i and perform the loop until the condition 'i < size' becomes false and increment the value of variable i in every iteration and in the block of the loop we have to check the condition 'arr[i] % 2 == 0' if true then assign the value of arr[i] into the array evenArray[j] and increment the value of j otherwise assign the value of arr[i] into the array oddArray[k] and increment the value of k. After the completion of the loop, we can print the elements of the array evenArray[] as the array with even numbers and elements of the array oddArray[] as the array with the odd numbers.

ALGORITHM

Step 1: Initialize an array arr[] with values

Step 2: Create two empty arrays oddArray[] and evenArray[] to store the odd and even numbers separately

Step 3: Assing the size of the array into the variable size using the built-in function count()

Step 4: Print the elements of the array arr[] using for loop

Step 5: Assign the value 0 into the variables j and k

Step 6: Assign the value 0 into the variable i and perform the sub-step until the condition 'i < size' becomes false and increment the value of variable i in every iteration

        (i) Check the condition 'arr[i] % 2 == 0' if true then assign the value of arr[i] into the array evenArray[j] and increment the value of j otherwise perform the sub-step ii

        (ii) Assign the value of arr[i] into the array oddArray[k] and increment the value of k

Step 7: Print the elements of array evenArray[] using for loop as the even numbers

Step 8: Print the elements of array oddArray[] using for loop as the odd numbers

PHP Source Code

                                          <?php
$arr = array(1, 2, 3, 4, 5, 6, 7, 8, 9);
$oddArray = array();
$evenArray = array();
echo "Orginal array: \n";
$size = count($arr);
for ($i = 0; $i < $size; $i++) {
    echo "$arr[$i] ";
}
$j = 0;
$k = 0;
for ($i = 0; $i < $size; $i++) {
    if ($arr[$i] % 2 == 0) {
        $evenArray[$j] = $arr[$i];
        $j++;
    } else {
        $oddArray[$k] = $arr[$i];
        $k++;
    }
}
echo "\nArray of even numbers : \n";
for ($i = 0; $i <= $j; $i++) {
    echo "$evenArray[$i] ";
}
echo "\nArray of odd numbers : \n";
for ($i = 0; $i <= $k; $i++) {
    echo "$oddArray[$i] ";
}
?>
                                      

OUTPUT

Orginal array:
1 2 3 4 5 6 7 8 9
Array of even numbers :
2 4 6 8
Array of odd numbers :
1 3 5 7 9