Total Sum of All Numbers in Array With Recursion.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public class Tester {
 
    //sum of all numbers in array with recursion
    public static int sumArray(int[]a, int i)
    {
        if(i == a.length-1)
            return a[i];
        else
            return a[i] + sumArray(a, ++i);
    }
 
    public static void main(String[] args) {
 
        int[] nums = {20, 44, 29, 55, 25};
 
        System.out.println(sumArray(nums, 0));
    }
}
Share This!

Leave a Reply

Your email address will not be published.