# C program to print combinations from three digits.

Q44. Write a C program to accept three digits (that is, 0-9) and print all possible combinations from these digits. (For example, if the three digits are 1, 2 and 3 then all possible combinations are 123, 132, 231, 213, 312 and 321).

Ans.

#include<stdio.h>  //for standard input and output

#include<conio.h>  //for clrscr();

//Main Function

void main()

{

int a[3],i,n=0,swap;

clrscr();  //for clear screen

printf(“\n Enter three numbers: “);

//for loop to read three digits

for(i=0;i<3;i++)

{

scanf(“%d”,&a[i]);

}

printf(“\n All possible combinations of given three digits is:\n”);

for(i=0;i<=3;i++)

{

if(n==0)

{

n=a[0]*100+a[1]*10+a[2];

printf(“%d\n”,n);

}

else

{

swap=a[1];

a[1]=a[2];

a[2]=swap;

n=a[0]*100+a[1]*10+a[2];

printf(“%d\n”,n);

if(i!=3)

{

swap=a[0];

a[0]=a[1];

a[1]=swap;

n=a[0]*100+a[1]*10+a[2];

printf(“%d\n”,n);

}

}

}

getch();  //To hold the output screen

}

//End of main()

Output:

Enter three numbers: 1 2 3

All possible combinations of given three digits is:

123

132

312

321

231

213

185 Views