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 Right and Ones Left Side in Array

This C program that takes user input to create an array of binary digits (1s and 0s), checks the input for validity, and then sorts the array in descending order.

#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
10
Enter 1's and 0's only
0
1
1
1
0
1
0
1
1
0
1111000000
C Programming Examples

Let’s break down the code step by step:

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

These lines include standard input/output and standard library header files, which are necessary for functions like printf, scanf, and exit.

  1. Main Function:
   main()

In C, main() is the entry point of the program.

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

Declares four integer variables: i for loop counters, size to store the size of the array, temp for swapping elements during sorting, and j for nested loop.

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

Asks the user to enter the size of the array and stores it in the variable size.

  1. Array Declaration:
   int a[size];

Declares an array a of size specified by the user input.

  1. User Input for Binary Digits:
   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);
       }
   }

Asks the user to input binary digits (1s and 0s) into the array. It checks each input and exits the program if a non-binary digit is entered.

  1. Sorting the Array in Descending Order (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;
           }
       }
   }

Uses the Bubble Sort algorithm to sort the array in descending order.

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

Prints the sorted array to the console.

This C program takes user input to create an array of binary digits, checks the input for validity, sorts the array in descending order using the Bubble Sort algorithm, and then displays the sorted array.

Spread the love
Scroll to Top
Scroll to Top