Sahib Za Khan Sahib Za Khan - 24 days ago 9
Java Question

How can I call a recursive function in reverse order

I was asked an interview question stating that how you can print number from one to the supplied parameter number to a recursive function. Say the function name is recursiveFunc When I call it like

recursiveFunc(100);

It should print 1,2,3 to 100 and so on.

Note it should not use any other helping initializing etc variable.

Any thoughts?.....

Answer
public void recursiveFunc(int n) {
    if (n <= 0) 
       return;
    recursiveFunc(n-1); 
    System.out.println(n); 
}

Print after recursion.