Sum of array elements using recursion

27 Jan, 2022
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 arrayIn this post, recursive solution is discussed.  
C++
Java
Python3
C#
PHP
Javascript
// 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 codeint main(){    int A[] = { 1, 2, 3, 4, 5 };    int N = sizeof(A) / sizeof(A[0]);    printf("%dn", findSum(A, N));    return 0;}
// 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));    }}
# 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 codearr =[]# input values to listarr = [1, 2, 3, 4, 5]  # calculating length of arrayN = len(arr)  ans =_findSum(arr,N)print (ans)  # This code is contributed by Khare Ishita
// 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.
<?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?>
<script> // JavaScript 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 let A = [1, 2, 3, 4, 5];let N = A.length;document.write(findSum(A, N)); </script>
Output:  
Chapters
descriptions off, selected
captions settings, opens captions settings dialog
captions off, selected
English
This is a modal window.
Beginning of dialog window. Escape will cancel and close the window.
End of dialog window.
15
How does above recursive solution work?  
 
Sum of array elements using recursion | GeeksforGeeks - YouTubeGeeksforGeeks529K subscribersSum of array elements using recursion | GeeksforGeeksWatch laterShareCopy linkInfoShoppingTap to unmuteIf playback doesn't begin shortly, try restarting your device.More videosMore videosYou're signed outVideos you watch may be added to the TV's watch history and influence TV recommendations. To avoid this, cancel and sign in to YouTube on your computer.CancelConfirmSwitch cameraShareInclude playlistAn error occurred while retrieving sharing information. Please try again later.Watch on0:000:000:00 / 2:01•Live•<div class="player-unavailable"><h1 class="message">An error occurred.</h1><div class="submessage"><a href="https://www.youtube.com/watch?v=-tqsxDpp6NI" target="_blank">Try watching this video on www.youtube.com</a>, or enable JavaScript if it is disabled in your browser.</div></div>
This article is contributed by Prakhar Agrawal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@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.
Khare Ishita
nitin mittal
ihritik
_saurabh_jaiswal
amartyaghoshgfg
Arrays
School Programming
Arrays
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Maximum and minimum of an array using minimum number of comparisons
Top 50 Array Coding Problems for Interviews
Multidimensional Arrays in Java
Stack Data Structure (Introduction and Program)
Linear Search
Python Dictionary
Reverse a string in Java
Introduction To PYTHON
Interfaces in Java
Inheritance in C++