I want to separate even and odd elements of array and print them.What's wrong with this code?

0 votes
asked Mar 7, 2019 by anonymous
#include<stdio.h>
void main()
{
    int a[5]={0,2,3,4,5};
    int i,x=0,y=0;
    
    for (i=0; i<=4; i++)
    {
        if( a[i] % 2 == 0 )
        {
            a[x] = a[i];
            x++;
        }
        else
        {
            a[y] = a[i];
            y++;
          
        }    
            
    }
    for (i=0; i<x; i++)
    {
        printf ("%d ", a[i]);
    }
    printf ("\n");
    for (i=0; i<y; i++)
    {
        printf ("%d ", a[i]);
   }
}

3 Answers

–1 vote
answered Mar 7, 2019 by Andreis (120 points)

#include<stdio.h>
void main()
{
    int a[5]={0,2,3,4,5};
    int i,x[5],y[5],j=0,k=0;
    
    for (i=0; i<=4; i++)
    {
        if( a[i] % 2 == 0 )
        {
            x[i] = a[i];

           j++;

        }
        else
        {
            y[i] = a[i];

            k++;          
        }    
    }
    for (i=0; i<j; i++)
    {
        printf ("%d ", x[i]);
    }
    printf ("\n");
    for (i=0; i<k; i++)
    {
        printf ("%d ", y[i]);
   }
}

commented Mar 8, 2019 by anonymous
0 2 4196045
4195968 0
this is the output on running this program.
commented Mar 8, 2019 by anonymous
#include<stdio.h>
void main()
{
    int a[5]={0,2,3,4,5};
    int i,j=0,k=0;
    int x[5];
    int y[5];
    for (i=0,j=0,k=0; i<=4; i++)
    {
        if( a[i] % 2 == 0 )
        {
            x[j] = a[i];

           j++;

        }
        else
        {
            y[k] = a[i];

            k++;          
        }    
    }
  
  for (i=0;i<=j;i++)
  {
      printf("%d ", x[i]);
  }
  printf("\n");
  for (i=0;i<=k;i++)
  {
  printf("%d ", y[i]);
  
  }
}
+1 vote
answered Mar 8, 2019 by anonymous

a[x] = a[i]; -- By doing this you are just adding a value to the different index of the same array.It will not work. To separate value of array you need to create two different arrays. Look at the below code.

#include<stdio.h>
void main()
{
    int a[5]={0,2,3,4,5};
    int i,j=0,k=0;
    int x[5];   
    int y[5];
    for (i=0,j=0,k=0; i<=4; i++)
    {
        if( a[i] % 2 == 0 )
        {
            x[j] = a[i];
           j++;

        }
        else
        {
            y[k] = a[i];
            k++;          
        }    
    }
  
  for (i=0;i<=j;i++)
  {
      printf("%d ", x[i]);
  }
  printf("\n");
  for (i=0;i<=k;i++)
  {
  printf("%d ", y[i]);
  
  }
}

The output of the above code is

0 2 4 0   

3 5 4195472

please ignore the garbage values.

0 votes
answered Mar 8, 2019 by Nipun
#include <stdio.h>

int main()
{
    int arr[10] = {1,2,3,4,5,6,7,8,9,10};
    int *ptr = arr;
    while(*ptr != NULL)
    {
        if(*ptr % 2 == 0)
        {
            printf( "\nnumber is even and value is %d",*ptr);
        
        }
        else
        {
        printf( "\nnumber is odd and value is %d",*ptr);
        }
        ptr++;
    }
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...