Write a c++ program to accept three digits (i.e 0-9)and print all possible combinations fro these digits.(for exanple if the three digits are 1,2,3 then all possible combinations are 123,132,231,213,312,321 etc)

Showing Answers 1 - 15 of 15 Answers

Yanesh Tyagi

  • Dec 23rd, 2006
 

/* Program to write combination of 3 digit number
   Author : Rachit Tyagi
   Date : 23-Dec-2006 2345 IST
*/
 
#include<iostream.h>
#include<conio.h>
void main(){
 clrscr();
 const int s = 3;
 int a[s];
 int x,y,z;
 for (int i=0; i<s; i++)
  cin >> a[i];
  x = a[0];
  y = a[1];
  z = a[2];
  i = 0;
  cout<<x<<y<<z;
  cout<<"n";
  while(1){
  a[s] = a[i];
  if (i != s - 1){
   a[i] = a[i+1];
   a[i+1] = a[s];
  }
  else {
   a[i] = a[0];
   a[0] = a[s];
   i=-1;
  }
  i++;

  if (a[0] == x && a[1] == y && a[2] == z)
   break;

  for (int j = 0; j < s; j++)
   cout<<a[j];
  cout<<"n";

  }
  getch();

}

  Was this answer useful?  Yes

int a[3]; a[0] = 1; a[1] = 2; a[2] = 3; // Print all the permutations of a three digit number for (int i = 0; i<=2;i++) { for (int j = 0; j<=2;j++) { if(i!=j) { printf("%d",a[i]); printf("%d",a[j]); for (int k = 0; k<=2;k++) { if((i!=k)&& j!=k) { printf("%dn",a[k]); } } } } }

  Was this answer useful?  Yes

sreerag

  • Jul 3rd, 2013
 

Code
  1. #include<iostream.h>

  2. void main()

  3. {

  4.         int z,a,b,c,q,w,e,r,t;

  5.         cout<<"enter a three digit number";

  6.         cin>>z;

  7.         a=z/100;

  8.         c=z%10;

  9.         b=(z-(a*100+c))/10;

  10.         z=a*100+b*10+c;

  11.         cout<<"first="<<z<<"

  12. ";

  13.         q=a*100+c*10+b;

  14.         cout<<"second="<<q<<"

  15. ";

  16.         w=b*100+c*10+a;

  17.         cout<<"third="<<w<<"

  18. ";

  19.         e=b*100+a*10+c;

  20.         cout<<"fourth="<<e<<"

  21. ";

  22.         r=c*100+a*10+b;

  23.         cout<<"fifth="<<r<<"

  24. ";

  25.         t=c*100+b*10+a;

  26.         cout<<"sixth="<<t;

  27. }

  Was this answer useful?  Yes

adnan

  • Jan 13th, 2016
 

Code
  1. Yanesh Tyagi

  2.  Dec 23rd, 2006

  3.  

  4. /* Program to write combination of 3 digit number

  5.    Author : Rachit Tyagi

  6.    Date : 23-Dec-2006 2345 IST

  7. */

  8.  

  9. #include<iostream.h>

  10. #include<conio.h>

  11. void main(){

  12.  clrscr();

  13.  const int s = 3;

  14.  int a[s];

  15.  int x,y,z;

  16.  for (int i=0; i<s; i++)

  17.   cin >> a[i];

  18.   x = a[0];

  19.   y = a[1];

  20.   z = a[2];

  21.   i = 0;

  22.   cout<<x<<y<<z;

  23.   cout<<"n";

  24.   while(1){

  25.   a[s] = a[i];

  26.   if (i != s - 1){

  27.    a[i] = a[i+1];

  28.    a[i+1] = a[s];

  29.   }

  30.   else {

  31.    a[i] = a[0];

  32.    a[0] = a[s];

  33.    i=-1;

  34.   }

  35.   i++;

  36.   if (a[0] == x && a[1] == y && a[2] == z)

  37.    break;

  38.   for (int j = 0; j < s; j++)

  39.    cout<<a[j];

  40.   cout<<"n";

  41.   }

  42.   getch();

  43. }

  Was this answer useful?  Yes

Lineesh K Mohan

  • Mar 22nd, 2016
 

Code
  1. #include<stdio.h>

  2. #include<iostream>

  3.  

  4. using namespace std;

  5.  

  6. int main()

  7. {

  8.         int arrNum[3] = {0};

  9.         int nTemp = 0;

  10.         int nI = 0,nJ = 0,nK = 0;

  11.  

  12.         cout<<"

  13.  Enter the 3 digit numbers      ";

  14.         cin>>nTemp;

  15.  

  16.         arrNum[0] = nTemp/100;

  17.         arrNum[1] = (nTemp/10) - (arrNum[0] * 10);

  18.         arrNum[2] = nTemp%10;

  19.  

  20.         for(nI = 0; nI < 3; nI++)

  21.         {

  22.                 nJ = (nI+1) % 3;

  23.                 nK = (nI+2) % 3;

  24.                 nTemp = 100 * arrNum[nI] + 10 * arrNum[nJ] + arrNum[nK];

  25.                 cout<<nTemp<<endl;

  26.                 nTemp = 100 * arrNum[nI] + 10 * arrNum[nK] + arrNum[nJ];

  27.                 cout<<nTemp<<endl;

  28.         }

  29.         return 0;

  30. }

  31.  

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions