Skip to content

Commit fdf1017

Browse files
committed
sum of first N natural number using recursion
1 parent 1c3a0e2 commit fdf1017

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package Recursion;
2+
3+
public class sumOfNTerms {
4+
5+
public static void main(String[] args) {
6+
7+
int n = 10;
8+
System.out.println(nthSum(n)); // 55
9+
System.out.println(nthSum(3)); // 6
10+
}
11+
12+
public static int nthSum(int n) {
13+
if (n == 0) {
14+
return 0;
15+
}
16+
return n + nthSum(n - 1);
17+
}
18+
}

0 commit comments

Comments
 (0)