So I have this assignment in my computer algorithm class: Write a recursive algorithm that, given a positive integer n >= 1, prints all sequences of numbers k >= 1 and 1 <= i1 < i2 <...< ik <= n. For example: if n=3, then the output will be
1
1,2
1,2,3
1,3
2
2,3
3
I was trying to write the recursive code for this assignment in Java but I do not know how to approach this problem. I understand the basics of recursion but I have trouble writing recursive code by myself.
this is what I have right now:
public class question4
{
public static void main(String arg[]){
int x = 10;
printSequence(x);
}
public static int printSequence(int n){
if (n == 1){
System.out.println(n);
return n;
}
else{
int result = printSequence(n-1) + 1;
System.out.println(result);
return result;
}
}
}
It only prints 1,2,3,4,5,6,7,8,9,10
Please help me!
Thank you in advance!