-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRecursion.java
More file actions
25 lines (25 loc) · 927 Bytes
/
Recursion.java
File metadata and controls
25 lines (25 loc) · 927 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
//Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:
public class Recursion {
public static void main(String[] args) {
int result = sum(10);
System.out.println(result);
int resultn = sumn(5,10);
System.out.println(resultn);
}
//Use recursion to add all numbers up to 10
public static int sum(int k) {
if(k>0) {
return k + sum(k-1);
} else {
return 0;
}
}
//Use recursion to add all numbers between start to end
public static int sumn(int start, int end) {
if(end > start) {
return end + sumn(start, end - 1);
} else {
return end;
}
}
}