C Program to Seperate Even & Odd Elements of an Array

In this program we find the odd and even elements of an array and insert them into two different arrays.

Here the logic of the program to find whether a number is odd or even is used.

//Program

#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]);


}


}

Comments

Popular posts from this blog

Algorithm to display "n" natural numbers #c

Algorithm to display the sum of n natural numbers #c

Algorithm for Sum of two numbers