I cannot figure out the programme code for the following problem:
Write aq programe code for printing all the combinations of the numbers 1,2&3.

I would be gratefull if you can help me out with this.
Thank you.

8 Years
Discussion Span
Last Post by Adak

You have a 1 in the 100's column, a 2 in the 10's column, and a 3 in the 1's column.

So you could look to swap the columns:

1 2 3 
1 3 2
3 1 2
3 2 1
2 3 1
2 1 3

Another way to look at it (less efficient, but simpler in concept perhaps), is to make your set of {1,2,3} into an odometer on a car.

Think of it as having three wheels, and on these odometer wheels, we have only 3 digits that they can show - either 1, 2, or 3.

| 1 | 2 | 3 |

now move the odometer wheels, like the car was running down the road:

| 1 | 2 | 4 | <== can't show a 4, so this wheel becomes a 1
| 1 | 3 | 1 | <== middle wheel is incremented, but we can't have 2 1's
| 1 | 3 | 2 | <== got the next combination

continue incrementing in this way ALWAYS working from the right most "wheel", then the middle wheel, then the left most wheel, just like a real odometer would. Set up your logic, so only the digits one two or three can be valid, and only one of each digit can valid.
This can work out well with recursion or nested for loops. The left wheel is the outermost for loop, the middle wheel is the middle for loop, and the right most wheel is the innermost for loop.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.