Separate even and odd numbers in array: To separate even and odd numbers from an array.

Even numbers:
The numbers which are divisible by 2.
The numbers 2, 4, 6 … are even numbers

Odd numbers
The numbers which are divisible by 2 gives the fraction result.
The numbers 3, 6, 9… are Odd numbers.

Algorithm to separate even and odd numbers in array

  1. Take input size of array. N
  2. Take input numbers from the user. a[N]
  3. Traverse array to print even numbers
    For i = 0 to end
    If a[i] % 2 == 0
    Print even number
  4. Traverse array to print odd numbers
    For i = 0 to end
    If a[i] % 2 == 1
    Print odd number

C program to separate even and odd numbers in array

#include<stdio.h>
main()
{
int i,size;
printf("Enter size of array\n");
scanf("%d",&size);
int a[size];
printf("Enter numbers to separate even and odd\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
}
printf("Even numbers are:\n");
for(i=0;i<size;i++)
{
if(a[i]>=0)
{
if(a[i]%2==0)
{
printf("%d\n",a[i]);
}

}
}

printf("Odd numbers are:\n");
for(i=0;i<size;i++)
{
if(a[i]>=0)
{
if(a[i]%2==1)
{
printf("%d\n",a[i]);
}

}
}

}

Output :

Separate even and odd numbers in array

Write A Comment