Sum of array elements using recursion
Given an array of integers, find sum of array elements using recursion.
Examples:
Input : A[] = {1, 2, 3}
Output : 6
1 + 2 + 3 = 6
Input : A[] = {15, 12, 13, 10}
Output : 50
We have discussed iterative solution in below post.
Sum of elements in a given array
In this post, recursive solution is discussed.
C++
// C++ program to find sum of array // elements using recursion. #include <stdio.h> // Return sum of elements in A[0..N-1] // using recursion. int findSum(int A[], int N) { if (N <= 0) return 0; return (findSum(A, N - 1) + A[N - 1]); } // Driver code int main() { int A[] = { 1, 2, 3, 4, 5 }; int N = sizeof(A) / sizeof(A[0]); printf("%dn", findSum(A, N)); return 0; } |
chevron_right
filter_none
Java
// 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..N-1] // using recursion. static int findSum(int A[], int N) { if (N <= 0) return 0; return (findSum(A, N - 1) + A[N - 1]); } // Driver method public static void main(String[] args) { System.out.println(findSum(arr, arr.length)); } } |
chevron_right
filter_none
Python
# Python program to find sum of array # elements using recursion. # Return sum of elements in A[0..N-1] # using recursion. def _findSum(arr, N): if len(arr)== 1: return arr[0] else: return arr[0]+_findSum(arr[1:], N) # driver code arr =[] # input values to list arr = [1, 2, 3, 4, 5] # calculating length of array N = len(arr) ans =_findSum(arr,N) print (ans) # This code is contributed by Khare Ishita |
chevron_right
filter_none
C#
// C# program to find sum of array // elements using recursion. using System; class Test { static int []arr = {1, 2, 3, 4, 5}; // Return sum of elements in // A[0..N-1] using recursion. static int findSum(int []A, int N) { if (N <= 0) return 0; return (findSum(A, N - 1) + A[N - 1]); } // Driver Code public static void Main() { Console.Write(findSum(arr, arr.Length)); } } // This code is contributed by Nitin Mittal. |
chevron_right
filter_none
PHP
<?php // PHP program to find sum // of array elements using // recursion. // Return sum of elements // in A[0..N-1] using recursion. function findSum($A, $N) { if ($N <= 0) return 0; return (findSum($A, $N - 1) + $A[$N - 1]); } // Driver code $A = array(1, 2, 3, 4, 5); $N = sizeof($A); echo findSum($A, $N); // This code is contributed // by ihritik ?> |
chevron_right
filter_none
Output:
15
How does above recursive solution work?

This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Recommended Posts:
- Sum of even elements of an Array using Recursion
- Sum of array Elements without using loops and recursion
- Mean of array using recursion
- C++ Program to print an Array using Recursion
- Program to check if an array is palindrome or not using Recursion
- Sum of elements in 1st array such that number of elements less than or equal to them in 2nd array is maximum
- Find original array from encrypted array (An array of sums of other elements)
- Average of remaining elements after removing K largest and K smallest elements from array
- Find elements larger than half of the elements in an array
- Elements to be added so that all elements of a range are present in array
- Count array elements that divide the sum of all other elements
- Maximize the sum of X+Y elements by picking X and Y elements from 1st and 2nd array
- Replace array elements by sum of next two consecutive elements
- Count number of elements between two given elements in array
- Find Kth element in an array containing odd elements first and then even elements



