S
Sue
I have this code with nested for loops - I need to convert this to a
recursive function. There are 3 nested for loops so the results are 3
digit numbers, example 279: 289: 345: 346: 347: 348: 349: 356: 357:
358: 359: 367: 368: where the 2nd digit is always larger than the
first, and the 3rd digit is always larger than 2nd. If I need 4 digit
numbers then I have to write 4 for loops, 5 digits - 5 for loops and so
on.
If I convert this to a recursive function I should be able to pass the
number of digits needed, 3, 4, 5 etc as argument to the function.
I am finding it difficult to understand recursion so any help in the
right direction will be extremely helpful. Thank you very much!
int a[] = {0, 1,2,3,4,5,6,7,8,9};
for (int i =0; i<a.length; i++)
for (int j=0; j<a.length; j++)
for (int k=0; k<a.length; k++)
{
if (a[j]>a & a[k]>a[j] ) {
System.out.println(a + "" + a[j] + "" +
a[k]);
}
}
recursive function. There are 3 nested for loops so the results are 3
digit numbers, example 279: 289: 345: 346: 347: 348: 349: 356: 357:
358: 359: 367: 368: where the 2nd digit is always larger than the
first, and the 3rd digit is always larger than 2nd. If I need 4 digit
numbers then I have to write 4 for loops, 5 digits - 5 for loops and so
on.
If I convert this to a recursive function I should be able to pass the
number of digits needed, 3, 4, 5 etc as argument to the function.
I am finding it difficult to understand recursion so any help in the
right direction will be extremely helpful. Thank you very much!
int a[] = {0, 1,2,3,4,5,6,7,8,9};
for (int i =0; i<a.length; i++)
for (int j=0; j<a.length; j++)
for (int k=0; k<a.length; k++)
{
if (a[j]>a & a[k]>a[j] ) {
System.out.println(a + "" + a[j] + "" +
a[k]);
}
}