Created
August 19, 2016 12:48
-
-
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)…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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