Sunday 15 December 2013

C Program to Put Even & Odd Elements of an Array in 2 Separate Arrays

#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 < j; i++)
{
printf("%ld\n", OAR[i]);
}
printf("The elements of EAR are \n");
for (i = 0; i < k; i++)
{
printf("%ld\n", EAR[i]);
}
}

Output

 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
1
32768
11542516
11210377
The elements of EAR are
34

For More Details Please Visit Ictjobs.info

No comments: