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 out
You can also do it with a single (not so elegant) recursion,as follows:
public static void printTriangle (int leftInLine, int currLineSize, int leftLinesCount) {
if (leftLinesCount == 0)
return;
if (leftInLine == 0){ //Completed current line?
System.out.println();
printTriangle(currLineSize+1, currLineSize+1, leftLinesCount-1);
}else{
System.out.print("*");
printTriangle(leftInLine-1,currLineSize,leftLinesCount);
}
}
public static void printTriangle(int size){
printTriangle(1, 1, size);
}
The idea is that the method params represent the complete drawing state.
Note that size must be greater than 0.