C Program to Print Even and Odd Numbers in an Array

Write a C program to print even and odd numbers in an array. If a number is divisible by 2 then the number is even else the number is odd. To display the even and odd numbers in an array, first of all, initialize array and then check each element of the array.

``````#include<stdio.h>
int main()
{
int n, a[20];

printf("Enter the size of the array: ");
scanf("%d", &n);

printf("Enter array elements: \n");
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

printf("Even numbers in the array are: \n");
for(int i=0; i<n; i++)
{
if(a[i]%2==0)
printf("%d ", a[i]);
}

printf("\nOdd numbers in the array are: \n");
for(int i=0; i<n; i++)
{
if(a[i]%2!=0)
printf("%d ", a[i]);
}

return 0;
}``````

Output:-

Enter the size of the array: 5
Enter array elements:
10
15
20
54
26
Even numbers in the array are:
` 10 20 54 26 `
Odd numbers in the array are:
15

If you only want to separate the odd and even numbers in an array check the below program.

``````#include<stdio.h>
int main()
{
int n, a[20];

printf("Enter the size of the array: ");
scanf("%d", &n);

printf("Enter array elements: \n");
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

for(int i=0; i<n; i++)
{
if(a[i]%2==0)
printf("%d: Even\n", a[i]);
else
printf("%d: Odd\n", a[i]);
}

return 0;
}``````

Output:-

Enter the size of the array: 5
Enter array elements:
10
11
15
48
50
10: Even
11: Odd
15: Odd
48: Even
50: Even

C program to print even and odd numbers in an array using functions

``````#include<stdio.h>
void findEvenNumbers(int a[], int size)
{
for(int i=0; i<size; i++)
{
if(a[i]%2 == 0)
printf("%d ", a[i]);
}
}

void findOddNumbers(int a[], int size)
{
for(int i=0; i<size; i++)
{
if(a[i]%2 != 0)
printf("%d ", a[i]);
}
}

int main()
{
int n, a[20];

printf("Enter the size of the array: ");
scanf("%d", &n);

printf("Enter array elements: \n");
for(int i=0; i<n; i++)
{
scanf("%d",&a[i]);
}

printf("Even numbers in the array are: \n");
findEvenNumbers(a, n);

printf("\nOdd numbers in the array are: \n");
findOddNumbers(a, n);

return 0;
}``````

Output:-

Enter the size of the array: 5
Enter array elements:
11
12
13
15
18
Even numbers in the array are:
` 12 18 `
Odd numbers in the array are:
` 11 13 15`

In this program, we created two user-defined functions find Even Numbers and find Odd Numbers. The find Even Numbers finds the even number from the array and similarly, the user-defined function finds Odd Numbers find the odd number from the array.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do you find anything incorrect? Let us know in the comments. Thank you!