-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNaturalNumberRecursion.java
More file actions
21 lines (18 loc) · 683 Bytes
/
NaturalNumberRecursion.java
File metadata and controls
21 lines (18 loc) · 683 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import java.util.Scanner;
public class NaturalNumberRecursion {
public static int sumNaturalNumber(int n){
if (n == 1)
return 1;
return n + sumNaturalNumber(n - 1); //recursion function calling
}
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.println("Enter the last limit to sum: ");
int n=s.nextInt();
// NaturalNumberRecursion obj = new NaturalNumberRecursion();
// int result = obj.sumNaturalNumber(n);
//make method static then no need to create obj
int result = sumNaturalNumber(n);
System.out.println("Sum : " + result );
}
}