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

It is an array based C program, we have an array of integers and we need to extract the odd elements and even elements separately from it and add them to another array. Before discussing the program we should have knowledge of the following topics

## 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 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 depending on whether it is odd or even.

In this program, we need three arrays 1.to receive the user input 2. odd array 3. even array

• Using a loop traverse the user given array and check remainder of each element by dividing it with 2
• If remainder is 1, it is odd add to Odd array
• If remainder is 0, it is even add to Even array

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

The explanation of this program is here. 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 display arrays.

### ALGORITHM

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

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 Array 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 the Array 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`.

## 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
```
VIEW ALL
VIEW ALL
VIEW ALL