C program to remove even numbers and fill it with zero
Input array 1,2,3,4,5,6
Output 1,3,5,0,0,0

#include<stdio.h>
void main()

int i,j,k,n=6,a[]={1,2,3,4,5,6};
clrscr();
for(i=0;i<n;i++)
{
if(a[i]%2==0)
{
a[i]=-1;
}
else
printf("%d",a[i]);
}
for(i=0;i<n;i++)
{
if(a[i]==-1)
printf("0");
 }
getch();
}

Recommended Answers

Can you do the loop step 2? Would be more efficient.

Jump to Post

You say it will remove the even numbers and zero fill, but it doesn't. At the start the array looks like this:
{1,2,3,4,5,6}
and at the end it looks like this:
{1,-1,3,-1,5,-1}

Even numbers not removed (but instead replaced with -1), array not zero-filled. The program does not do what it is …

Jump to Post

All 5 Replies

Can you do the loop step 2? Would be more efficient.

You say it will remove the even numbers and zero fill, but it doesn't. At the start the array looks like this:
{1,2,3,4,5,6}
and at the end it looks like this:
{1,-1,3,-1,5,-1}

Even numbers not removed (but instead replaced with -1), array not zero-filled. The program does not do what it is supposed to.

but at last the expected output getting..

but at last the expected output getting..

If that's all that matters, here's the programme in a much more efficient form:

#include <stdio.h>

int main()
{
  printf("1 3 5 0 0 0");
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.