Consider this problem:
We have triangle made of blocks. The topmost row has 1 block, the next row down has 2 blocks, the next row has 3 blocks, and so on. Compute recursively (no loops or multiplication) the total number of blocks in such a triangle with the given number of rows.
My approach to this problem was this
public int triangle(int rows) {
if(rows == 0 || rows == 1 ) {
return rows;
} else {
return triangle(rows-1) +2;
}
}
The program works fine for values up to the number 3 but then the values that should come out are wrong (the calculation is wrong/recursive call). What am I missing?
Thank you in advance!