This is a C Program to put even & odd elements of an array in 2 separate arrays.
The program first finds the odd and even elements of the array. Then the odd elements of an array is stored in one array and even elements of an array is stored in another array.
1. Create three integer array. First to store all the elements. Second to store even elements of first array. Third to store odd elements of first array.
2. Take size of the array as input from users and define the first array by inserting elements of the array.
3. Using for loop, scan each and every element of the first array, check whether they are even or odd by taking modulo of 2 on those numbers.
4. Even numbers will get stored(copied) in second array, while odd numbers will get stored in third array.
5. Print both arrays.
Here is source code of the C program to put even & odd elements of an array in 2 separate arrays. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.
/*
* C Program to accept N integer number and store them in an array AR.
* The odd elements in the AR are copied into OAR and other elements
* are copied into EAR. Display the contents of OAR and EAR.
*/
#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("%ldn", OAR[i]);
}
printf("The elements of EAR are n");
for (i = 0; i < j; i++)
{
printf("%ldn", EAR[i]);
}
}
1. Create three integer arrays namely ARR, OAR, EAR of some fixed capacity, 10.
2. Take size of the array as input from users and define the first array, ARR by inserting elements of the array.
3. Using for loop, scan each and every element of the first array, check whether they are even or odd by taking modulo of 2 on those numbers.
4. If the array element modulo 2, returns 0, this means the number is divisible by 2 and it is an even number. Add this number to second array.
5. If the array element modulo 2, returns 1, this means the number is not divisible by 2 and it is an odd number. Add this number to third array.
6. Repeat steps 4 and 5 until all the elements of first array are checked.
7. At last, we will be having arrays second third holding all the even and odd elements of first array respectively.
8. Print both the OAR AND EAR.
9. Exit.
Enter the size of array AR 6 Enter the elements of the array 34 56 78 90 12 39 The elements of OAR are 39 The elements of EAR are 34 56 78 90 12
Sanfoundry Global Education & Learning Series – 1000 C Programs.
Here’s the list of Best Books in C Programming, Data Structures and Algorithms.
- Practice BCA MCQs
- Apply for C Internship
- Check Computer Science Books
- Watch Advanced C Programming Videos
- Check C Books