Take as input N, a number. Write a recursive function to find Nth triangle where 1st triangle is 1, 2nd triangle is 1 + 2 = 3, 3rd triangle is 1 + 2 + 3 = 6, so on and so forth. Print the value returned.
Input Format
Integer N is the single line of input.
Constraints
1 <= N <= 100
Output Format
Print the output as a single integer which is the nth triangle.
Sample Input
3
Sample Output
6import java.util.*;
public class Main {
public static void main(String args[])
{
Scanner s=new Scanner(System.in);
int number=s.nextInt();
System.out.println(printTriangleRecursion(number));
s.close();
}
public static int printTriangleRecursion(int number)
{
if(number==0)
{
return 0;
}
return number+printTriangleRecursion(number-1);
}
}
0 Comments