Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Java program to find sum of array elements using recursion.
- class Test {
- static int arr[] = { 1, 2, 3, 4, 5 };
- // Return sum of elements in A[0..L-1] using recursion.
- static int findSum(int A[], int L)
- {
- if (L <= 0)
- return 0;
- return (findSum(A, L - 1) + A[L - 1]);
- }
- // Driver method
- public static void main(String[] args)
- {
- System.out.println(findSum(arr, arr.length));
- }
- }
Add Comment
Please, Sign In to add comment