Skip to content

Commit 440db4f

Browse files
authored
Added sum with recursion to 03_recursion, java folder (#254)
* added sum with recursion to 03_recursion, java folder * Update .gitignore * update list not to be modified during recursion * fixed update
1 parent 22c2355 commit 440db4f

File tree

1 file changed

+19
-0
lines changed

1 file changed

+19
-0
lines changed

Diff for: 03_recursion/java/04_sum/src/Sum.java

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import java.util.*;
2+
3+
public class Sum {
4+
public static int sum(ArrayList<Integer> num_list, int index) {
5+
6+
if (num_list.size() == index) {
7+
return 0;
8+
} else {
9+
int num = num_list.get(index);
10+
return num + sum(num_list, index + 1);
11+
}
12+
13+
}
14+
15+
public static void main(String[] args) {
16+
int total = sum(new ArrayList<Integer>(Arrays.asList(2, 4, 6)), 0);
17+
System.out.println(total);
18+
}
19+
}

0 commit comments

Comments
 (0)