How can I make a recursive version of my iterative method?

Posted by user247679 on Stack Overflow See other posts from Stack Overflow or by user247679
Published on 2010-03-27T23:57:46Z Indexed on 2010/03/28 0:03 UTC
Read the original article Hit count: 187

Filed under:
|
|
|
|

Greetings. I am trying to write a recursive function in Java that prints the numbers one through n. (n being the parameter that you send the function.) An iterative solution is pretty straightforward:

public static void printNumbers(int n){
    for(int i = 1; i <= n; i++){
         System.out.println(i);
         i++;
    }

As a novice programmer, I'm having troubles figuring out how a recursive version of this method would work. Any bright ideas? Thanks for reading my problem!

© Stack Overflow or respective owner

Related posts about recursion

Related posts about iterative