Page 1 of 1

### Finding permutations of any 3-digit number

Posted: Thu Apr 26, 2012 6:32 am
Now I was working on this problem in school where I had to list out all possible permutations of a given three digit number. Here's my code -

Code: Select all

``````Output "Enter a three-digit number: "
Input no
hundreds_digit=no/100,
ones_digit=no%10,
tens_digit=no/10-10*hundreds_digit``````
Now I've got the one's, ten's and the hundred's digit. The problem is, how do I display their different combinations other than manually write all six possible ways (that would be worse) like

Code: Select all

``````Output hundreds_digit, tens_digit, ones_digit
Output hundreds_digit, ones_digit, tens_digit``````
And so on...

Any help will be appreciated.

### Re: Finding permutations of any 3-digit number

Posted: Thu Apr 26, 2012 6:56 am
You'll need to use some data structure like an array or a list (or even a string) so that you can access those digits individually by index number, rather than by specific variable names for each digit. That way the digits will be something like digit[1], digit[2], and digit[3] (or digit[0], digit[1], digit[2] depending on the programming language).

This allows you to use a variable for the index, i.e. digit[index] where index is a variable with the value 1, 2, or 3 (or 0, 1, 2 as noted above). It gives much more flexibility.To do what you want, you can then use three nested for-loops, with if statements to avoid repeating a digit in one number.

If you then want to go on to make it work for numbers of any length, you will probably want to look at recursion.

### Re: Finding permutations of any 3-digit number

Posted: Thu Apr 26, 2012 7:14 am
Expand
I'm using C++. If I use an array to store each digit in each index then I'll have to input one digit and then other digit. What I mean to say is that I cannot input the whole number at a time. I'm in 12th grade so I haven't learnt advanced methods. Still I'll try considering your suggestion. Thanks.

### Re: Finding permutations of any 3-digit number

Posted: Thu Apr 26, 2012 7:40 am
nisargshah95 wrote:I'm using C++. If I use an array to store each digit in each index then I'll have to input one digit and then other digit. What I mean to say is that I cannot input the whole number at a time. I'm in 12th grade so I haven't learnt advanced methods. Still I'll try considering your suggestion. Thanks.

Code: Select all

``````int no;
// Input no - whatever code you use for inputting no goes here
// Assumption: At this point no is an integer between 100 and 999 inclusive.
int digits[3];
digits[0] = no/100;
digits[1] = no%10;
digits[2] = no/10-10*digits[0];``````

### Re: Finding permutations of any 3-digit number

Posted: Fri Apr 27, 2012 5:12 am
Yeah! That's what I was exactly going to suggest before I saw your post. And now I've solved my problem. Did the following -

Code: Select all

``````seq[3]={ones_digit,tens_digit,hundreds_digit}

for (int i=0;i<=2;i++)
Output seq[i], seq[(i+1)%3], seq[(i+2)%3]
Output seq[i], swap(seq[(i+1)%3],seq[(i+2)%3]) //Where 'swap' will swap the last two digits and return a two-digit number.``````
Thanks for helping me out!