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:

#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();
}

You might also like:

Program to print combination of 3 and 4

Follow For Instant Updates

Join WhatsApp Group: link
Join our Telegram Channel: link
Like our Facebook Page:  link
Subscribe to our Youtube channel: link

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.
0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x