Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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.
- Input
- On the first line you will be given the number of rows.
- Output
- On the only output line you should print the total number of blocks.
- Sample tests
- Input
- 0
- Output
- 0
- Input
- 1
- Output
- 1
- Input
- 3
- Output
- 6
- import java.util.Scanner;
- public class Triangle {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- int n = Integer.parseInt(scanner.nextLine());
- System.out.println(blocks(n));
- }
- public static int blocks(int n){
- if(n<2){
- return n;
- }
- return n + blocks(n-1);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment