C Program to put even and odd elements of array into two separate arrays


May 3, 2022, Learn eTutorial
1748

How to separate odd and even numbers in an array?

We have to separate the elements in an array as odd and even numbers and store the elements in two separate arrays. So we have to check each element or number in the main array and move the number to its respective array depending on whether it is odd or even.

In this program, we have to create 3 arrays  1. To receive the user input 2. For odd numbers 3. For even numbers

  • Using a loop, traverse the user-input array, divide each element with the number 2, and check the remainder
  • If the remainder is 1, add to Odd array
  • If the remainder is 0, add to Even array

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

To implement this C program we have to declare three Arrays namely input_arr[ ] will contain the numbers given by the user, odd_arr[ ] to store odd numbers from input_arr[ ]even_arr[ ] to store even numbers from input_arr[ ].

First, we have to read the Limit of the Array from the user and read the numbers into input_arr[ ]. Then by using a for loop check if input_arr[i] %2 == 0, if yes add number to even_arr[ ] otherwise to odd_arr[ ]. After checking all elements.

ALGORITHM

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

STEP 2: Declare the Arrays input_arr[ ]odd_arr[ ],even_arr[ ]

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

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

STEP 5: Read the elements of the into input_arr[ ] using for Loop.

STEP 6: By using for Loop with the condition i < n do step 7.

STEP 7: Check if the number input_arr[i]%2 == 0 if yes number is Even, then copy the number into even_arr[j] also increment j by 1, else do step 8.

STEP 8: The number is Odd, copy the number into odd_arr[k] and then increment k by 1.

STEP 9: Display elements of even_arr[ ] as Even Numbers by using a for Loop.

STEP 10: Display elements of odd_arr[ ] as Odd Numbers by using for Loop.


For separating the elements of an Array using C program we are using the below topics, please refer to these for a better understanding

C Source Code

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


                                      

OUTPUT

Enter the size of the array
5
Enter the elements of the array 
4
5
1
2
7
The elements of the Odd array are 
5
1
7
The elements of the Even array are 
4
2