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
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.
[quote="jaap"]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.[/quote]
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.
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.
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];
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.