Given an array from user the task is to arrange the numbers in such a way that the even numbers appear first in the array followed by odd numbers if any.
Example:
Consider an array of size 5.
| 2 | 3 | 4 | 1 | 8 |
Arranged array.
| 2 | 4 | 8 | 3 | 1 |
C
#include <stdio.h>
int main()
{
int n=0,c=0,temp=0;
printf("Enter the size of array: ");
scanf("%d",&n);
int ar[n];
printf("Enter the array elements:-\n");
for(int i=0;i<n;i++)
{
scanf("%d",&ar[i]);
}
for(int i=0;i<n;i++)
{
if(ar[i]%2==0)
{
temp=ar[c];
ar[c]=ar[i];
ar[i]=temp;
c++;
}
}
for(int i=0;i<n;i++)
{
if(ar[i]%2!=0)
{
temp=ar[c];
ar[c]=ar[i];
ar[i]=temp;
c++;
}
}
printf("Arranged array:-\n");
for(int i=0;i<n;i++)
{
printf("%d ",ar[i]);
}
return 0;
}C