As I understand, this problem is NOT about just printing "1 2 3 4 5 6 6 5 4 3 2 1" or "1 2 3 4 5 6 5 4 3 2 1". The number 6 is just to give an example of an integer.
Since noLoop is not successful, it is only natural to use a second recursion method. So noLoop and noLoop2 will do the job.
If you have to use one recursion method, an extra parameter such as StringBuilder result
might be needed to store what we got so far. That is how noLoop3 comes. (Another way is to use a static variable instead of the extra parameter. I will let you to figure that out.)
public class NoLoop {
public static void main(String[] args) {
noLoop(6);
noLoop2(6 - 1);
StringBuilder sb = new StringBuilder();
noLoop3(6, sb);
System.out.println(sb);
}
public static void noLoop(int n) {
if (n > 0) {
noLoop(n - 1);
System.out.println(n);
}
}
public static void noLoop2(int n) {
if (n > 0) {
System.out.println(n);
noLoop2(n - 1);
}
}
public static void noLoop3(int n, StringBuilder result ) {
if (n > 0) {
result.insert(0, n).append(n);
noLoop3(n - 1, result);
}
}
}
By the way, this question borders on programming, which is off-topic here. Please consider posting this kind of question to Stackoverflow later on.