Seperate Odd & Even Numbers - C Program

Program:
#include<stdio.h>
//#include<conio.h>
void main()
{
    int i,n,a[100],o[100],e[100],on=0,en=0;//clrscr();
    printf("Enter number of elements:");
    scanf("%d",&n);
    printf("Enter numbers into array:");
    for(i=0;i<n;i++)
        scanf("%d",&a[i]);
    for(i=0;i<n;i++)
    {
        if(a[i]==0)
            continue;
        if(a[i]%2==1)
        {
            o[on]=a[i];
            on++;
        }
        else
        {
            e[en]=a[i];
            en++;
        }
    }
    printf("\nOdd numbers:\n");
    for(i=0;i<on;i++)
        printf("%d\t ",o[i]);
    //printf("%d %d",on, en);
    printf("\nEven numbers:\n");
    for(i=0;i<en;i++)
        printf("%d\t",e[i]);
    printf("\n");
//    getch();
}

Output:
nn@linuxmint ~ $ gcc c14.c
nn@linuxmint ~ $ ./a.out
Enter number of elements:5
Enter numbers into array:5
4
3
2
0

Odd numbers:
5     3     
Even numbers:
4    2   
nn@linuxmint ~ $

1 comment:

  1. Immerse yourself in our Best Mern Stack Course In South Delhi , a program tailored to provide you with a comprehensive grasp of full-stack web development using the MERN technology.

    ReplyDelete

Related Posts Plugin for WordPress, Blogger...