C Program to put even and odd elements of array in to two seperate arrays

How to separate odd and even numbers in an array?

Here this c program needs to separate the elements in an array as odd and even numbers. Store the elements in two separate arrays means odd numbers into an odd array and even numbers in an even array. So what we need is to check each element or number in the main array and move the number to its respective array depends on odd or even.

In this c program, we are declaring three arrays, one as the main to receive the user input. Second and third for the odd and even numbers, respectively. After receiving the numbers from the user, add that to the main array.

Now we have to copy each element to their respective arrays, for that we need to check each number is odd or even. Opens a for loop, inside the for loop, we check each element's remainder using the mod operator with 2. Copy the number to the respective array depending on the value of the remainder.

How to Implement the odd, even Array logic in C?

The explanation of this program is to declare three Arrays. The 'ARR[]' will contain all the numbers given by the user, and the 'OAR[]' Array will contain the Odd numbers from 'ARR[],' 'EAR[]' will contain only the Even numbers from 'ARR[]'.

First, we have to read the Limit of the Array from the user and read the numbers into ARR[]. Then by using a For Loop check, each number in the Array is Odd or Even. If the number is Odd, then copy the number into 'ORR['] and increment the index 'j' by 1.

If the number is Even then, copy the number into the 'EAR[]' and increment index 'k' by 1. And repeat the Loop until we check all elements. Now the 'OAR[]' contains all Odd numbers, and 'EAR[]' contains all Even numbers of 'ARR[]'. Using a for Loop, the elements in 'OAR[]' and' EAR[] are displayed.

ALGORITHM

STEP 1: Include the header files to use the built-in functions in the C program.

STEP 2: Declare the Arrays 'ARR[]' ,'OAR[]','EAR[]'.

STEP 3: Declare the integer variable 'n, j, i'.

STEP 4: Accept the size of the Array into 'n'.

STEP 5: Read the elements of the Array into 'ARR[]' using for Loop.

STEP 6: By using for Loop with the condition 'i'.

STEP 7: If the number is Even means 'ARR[i]%2 == 0' then copy the number into the Array 'EAR[j]'.Else do step 9.

STEP 8: Then increment 'j' by 1.

STEP 9: If the number is Odd, copy the number into 'OAR[k]'. Then increment 'k' by 1.

STEP 10: Repeat step 6.

STEP 11: Display elements of 'EAR'  as Even Numbers by using a for Loop.

STEP 12: Display elements of 'OAR' as Odd Numbers by using for Loop.

C Source Code

                                              #include <stdio.h>
    void main()
    {
 
        long int ARR[10], OAR[10], EAR[10];
        int i, j = 0, k = 0, n;
 
        printf("Enter the size of array AR \n");
        scanf("%d", &n);
 
        printf("Enter the elements of the array \n");
        for (i = 0; i < n; i++)
        {
            scanf("%ld", & ARR[i]);
            fflush(stdin);
        }
 
        /*  Copy odd and even elements into their respective arrays */
 
        for (i = 0; i < n; i++)
        {
            if (ARR[i] % 2 == 0)
            {
                EAR[j] = ARR[i];
                j++;
            }
            else
            {
                OAR[k] = ARR[i];
                k++;
            }
        }
 
        printf("The elements of OAR are \n");
        for (i = 0; i < k; i++)
        {
            printf("%ld\n", OAR[i]);
        }
 
        printf("The elements of EAR are \n");
        for (i = 0; i < j; i++)
        {
            printf("%ld\n", EAR[i]);
        }
 
    }
                                      

OUTPUT

 Enter the size of array AR
 6

 Enter the elements of the array
 12
 345
 678
 899
 900
 111

The elements of OAR are
 345
 899
 111

 The elements of EAR are
 12
 678
 900