C Program To Split Even and Odd Elements of An Array Into Two Arrays

Lets write a C program to divide or split even and odd elements of an array into two separate arrays.

Example: Expected Input/Output

Enter 10 integer numbers
1
2
3
4
5
6
7
8
9
10

Array elements of even[5] are …
2
4
6
8
10

Array elements of odd[5] are …
1
3
5
7
9

Visual Representation

Split even and odd elements of array

Related Read:
C Program To Count Number of Even, Odd and Zeros In An Array

Video Tutorial: C Program To Split Even and Odd Elements of An Array Into Two Arrays


[youtube https://www.youtube.com/watch?v=lQCvBWMSvHY]

YouTube Link: https://www.youtube.com/watch?v=lQCvBWMSvHY [Watch the Video In Full Screen.]

Source Code: C Program To Split Even and Odd Elements of An Array Into Two Arrays

Method 1

#include<stdio.h>

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
        scanf("%d", &a[i]);

    for(i = 0; i < N; i++)
    {
        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\n", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\n", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
10
11
12
13
14
15
16
17
18
19

Array elements of even[5] are …
10
12
14
16
18

Array elements of odd[5] are …
11
13
15
17
19

Note: We’re assigning same size to all 3 array variables, because if user enters all odd numbers, then we need to transfer all the elements to array variable odd, for that we need the size of array variable odd to be same as that of the original array. Similarly, if user enters all even numbers we’ll need to have the same size for array variable even.

Logic To Split Even and Odd Elements of An Array Into Two Arrays

First we accept all the elements of an array from the user. Next we iterate through the array elements one by one using a for loop. Inside this for loop we check each individual element, if its even or odd. If a number is perfectly divisible by 2, then its even number else its odd number. If the fetched/selected number is even number, then we copy that number into array variable even, else we copy the element into array variable odd.

Next we use for loop to display the elements of array even and odd, which will have the even and odd elements of the original array.

Method 2

#include<stdio.h>
#include

#define N 10

int main()
{
    int a[N], even[N], odd[N], i, k1 = 0, k2 = 0;

    printf("Enter %d integer numbers\n", N);
    for(i = 0; i < N; i++)
    {
        scanf("%d", &a[i]);

        if(a[i] % 2 == 0)
            even[k1++] = a[i];
        else
            odd[k2++] = a[i];
    }

    printf("\n\nArray elements of even[%d] are ...\n", k1);
    for(i = 0; i < k1; i++)
        printf("%d\t", even[i]);

    printf("\n\nArray elements of odd[%d] are ...\n", k2);
    for(i = 0; i < k2; i++)
        printf("%d\t", odd[i]);

    printf("\n");

    return 0;
}

Output:
Enter 10 integer numbers
1
2
3
4
5
6
11
13
15
16

Array elements of even[4] are …
2
4
6
16

Array elements of odd[6] are …
1
3
5
11
13
15

In above source code we check if the number input by the user is even or odd immediately after the user enters a number. We don’t wait until user inputs all the elements of the array. This way we eleminate the need for a separate for loop to check even and odd elements in the original array and then assigning it to array variables even and odd. This is the best approach, which reduces resource usage and has faster execution time.

For list of all c programming interviews / viva question and answers visit: C Programming Interview / Viva Q&A List

For full C programming language free video tutorial list visit:C Programming: Beginner To Advance To Expert