Advertisement

/* Write a program in C to separate odd and even integers in separate arrays */
#include <stdio.h>
int main()
{
    int a[25],e[25],o[25],n,i,j=0,k=0;
    printf("Enter size of an array: ");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        printf("element - %d : ",i);
        scanf("%d",&a[i]);
    }
    for(i=0; i<n; i++)
    {
        if(a[i]%2==0)
            e[j++]=a[i];
        else
            o[k++]=a[i];
    }
    printf("Even numbers are: \n");
    for (i=0;i<j;i++)
        printf("%d ",e[i]);
    printf("\nOdd numbers are: \n");
    for (i=0;i<k;i++)
        printf("%d ",o[i]);

Output:

Enter size of an array: 5
element - 0 : 25
element - 1 : 36
element - 2 : 53
element - 3 : 108
element - 4 : 66
Even numbers are:
36 108 66
Odd numbers are:
25 53

Back

Post a Comment

0 Comments