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:
- Get the length of the combination from the user.
- Find the number of combinations using the pow() function.
- For all time i value is assigned to a.
- By using the value of a calculate the combinations.
- The binary values are stored in the array in reverse order.
- 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]