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
This commit is contained in:
Toheeb Oyekola
2024-12-07 14:43:11 +01:00
committed by GitHub
parent 22c23551cb
commit 440db4ff11

View File

@@ -0,0 +1,19 @@
import java.util.*;
public class Sum {
public static int sum(ArrayList<Integer> num_list, int index) {
if (num_list.size() == index) {
return 0;
} else {
int num = num_list.get(index);
return num + sum(num_list, index + 1);
}
}
public static void main(String[] args) {
int total = sum(new ArrayList<Integer>(Arrays.asList(2, 4, 6)), 0);
System.out.println(total);
}
}