C++ program to accept three digits (that is, 0-9) and print all possible combinations.

Q24. Write a 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<iostream.h>

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

 

//Main Function

void main()

{

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

clrscr();  //for clear screen

cout<<“\n Enter three numbers: “;

 

//for loop to read three digits

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

{

cin>>a[i];

}

 

cout<<“\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];

cout<<n<<“\n”;;

}

else

{

swap=a[1];

a[1]=a[2];

a[2]=swap;

 

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

cout<<n<<“\n”;

if(i!=3)

{

swap=a[0];

a[0]=a[1];

a[1]=swap;

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

cout<<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

Leave a Reply

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

%d bloggers like this: