C Program Arrange zeros to left side and ones right side in arrays
#include<stdio.h>
#include<stdlib.h>
main()
{
int i,size,j,zerosCount=0;
printf("Enter Size of Array\n");
scanf("%d",&size);
int a[size];
printf("Enter 1's and 0's only\n");
for(i=0;i<size;i++)
{
scanf("%d",&a[i]);
if(!(a[i]==0 || a[i]==1))
{
printf("Please Enter either 0 or 1\n");
exit(0);
}
}
for(i=0;i<size;i++)
{
if(a[i]==0)
{
zerosCount++;
}
}
for(i=0;i<size;i++)
{
if(i<zerosCount)
a[i]=0;
else
a[i]=1;
}
for(i=0;i<size;i++)
{
printf("%d",a[i]);
}
printf("\n");
}
Output:-
Enter Size of Array
10
Enter 1's and 0's only
0
1
1
1
0
1
0
1
1
0
0000111111
Post a Comment
If you have any doubts, Please let me know
Thanks!