Create a triangle out of stars using only recursion

Posted by Ramblingwood on Stack Overflow See other posts from Stack Overflow or by Ramblingwood
Published on 2010-04-26T21:41:02Z Indexed on 2010/04/26 21:43 UTC
Read the original article Hit count: 236

Filed under:
|
|

I need to to write a method that is called like printTriangle(5);. We need to create an iterative method and a recursive method (without ANY iteration). The output needs to look like this:

*
**
***
****
*****

This code works with the iterative but I can't adapt it to be recursive.

public void printTriangle (int count) {
    int line = 1;
    while(line <= count) {
        for(int x = 1; x <= line; x++) {
            System.out.print("*");
        }
        System.out.print("\n");
        line++;
    }
}

I should not that you cannot use any class level variables or any external methods.

Thanks.

© Stack Overflow or respective owner

Related posts about java

Related posts about recursion