Skip to content

Instantly share code, notes, and snippets.

@sbsatter
Created August 19, 2016 12:48
Show Gist options
  • Save sbsatter/dd72d6cb1729d6291ec90ffb0e50036d to your computer and use it in GitHub Desktop.
Save sbsatter/dd72d6cb1729d6291ec90ffb0e50036d to your computer and use it in GitHub Desktop.
CODINGBAT RECURSION 1 PROBLEM- JAVA- TRIANGLE 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. triangle(0) → 0 triangle(1) → 1 triangle(2)…
public int triangle(int rows) {
if(rows==0)
return 0;
if(rows==1){
return 1;
}
return triangle(rows-1)+rows;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment