To print the combinations 0’s and 1’s

To print the combinations 0’s and 1’s

This program can print the combinations of 0’s and 1’s for the given length.

Logic:

  • Get the length from the user.
  • Then find the number of combinations.
  • Calculate the combinational value using some conditions.
  • And then print the combinational values.

Algorithm:

  1. Get the length of the combination from the user.
  2. Find the number of combinations using the pow() function.
  3. For all time i value is assigned to a.
  4. By using the value of a calculate the combinations.
  5. The binary values are stored in the array in reverse order.
  6. So print the output from the end of the array.

Program:

[code lang=”c”]
#include<stdio.h>
#include<math.h>
void main()
{
int n,i,j,a,in[10];
clrscr();
printf("enter the length:");
scanf("%d",&n);
for(i=0;i<pow(2,n);i++) //pow function returns the number of combinations to print
{
a=i;
for(j=0;j<n;j++) //this loop can generate the combination of elements and stores in array
{
if(a%2==0)
{
in[j]=0;
a=a/2;
}
else
{
in[j]=1;
a=a/2;
}
}
for(j=n-1;j>=0;j–) //print the elements in reverse order
{
printf("%d ",in[j]);
}
printf("\n");
}
getch();
}
[/code]

You might also like:

Program to print combination of 3 and 4

Vignesh

A Computer Science graduate who likes to make things simpler. When he’s not working, you can find him surfing the web, learning facts, tricks and life hacks. He also enjoys movies in his leisure time.

Leave a Reply

Your email address will not be published. Required fields are marked *