Menu
×
Tutorials Ms Word Tutorial Ms Excel Tutorial Ms PowerPoint Tutorial C Language Tutorial C++ Tutorial C Sharp Tutorial Visual Basic Tutorial HTML Tutorial CSS Tutorial JavaScript Tutorial WordPress Tutorial
     ❯   

ADVERTISEMENT

Arrange Zeros (0) to left and Ones Right Side in Array

This C program that takes user input to create an array of 0s and 1s, then sorts and displays the array.

#include<stdio.h>
#include<stdlib.h>
main()
{
 int i,size,temp,j;
 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++)
 {
  for(j=i+1;j<size;j++)
  {
   if(a[i]>a[j])
   {
    temp=a[i];
    a[i]=a[j];
    a[j]=temp;
   }
  }
 }
 for(i=0;i<size;i++)
 {
  printf("%d",a[i]);
 }
 printf("\n");
}
C Programming Examples
OUTPUT
Enter Size of Array
5
Enter 1's and 0's only
1
1
0
1
0
00111
C Programming Examples

Let’s break down the code step by step:

  1. Header Files:
   #include<stdio.h>
   #include<stdlib.h>

These lines include the necessary header files for input/output operations and dynamic memory allocation functions.

  1. main Function:
   main()
   {

The program starts with the main function, which is the entry point of any C program.

  1. Variable Declarations:
   int i, size, temp, j;

Declare four integer variables: i for loop control, size for the size of the array, temp for temporary storage during sorting, and j for nested loop control.

  1. User Input for Array Size:
   printf("Enter Size of Array\n");
   scanf("%d", &size);

Prompt the user to enter the size of the array and read the input into the variable size.

  1. Array Declaration and Input Validation:
   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);
       }
   }

Declare an array a of size specified by the user. Use a loop to take input for each element, and if the input is not 0 or 1, print an error message and exit the program.

  1. Sorting the Array (Bubble Sort):
   for(i = 0; i < size; i++)
   {
       for(j = i + 1; j < size; j++)
       {
           if(a[i] > a[j])
           {
               temp = a[i];
               a[i] = a[j];
               a[j] = temp;
           }
       }
   }

Implement a simple sorting algorithm (Bubble Sort) to arrange the elements of the array in ascending order.

  1. Display Sorted Array:
   for(i = 0; i < size; i++)
   {
       printf("%d", a[i]);
   }
   printf("\n");

Display the sorted array by printing each element.

  1. End of Program:
   }

Close the main function.

The program prompts the user for the size of an array, validates that the elements entered are only 0s and 1s, sorts the array in ascending order, and finally prints the sorted array.

Spread the love
Scroll to Top
Scroll to Top