The Wayback Machine - https://web.archive.org/web/20240717181822/https://www.geeksforgeeks.org/sum-array-elements-using-recursion/
Open In App

Sum of array elements using recursion

Last Updated : 19 Sep, 2023
Improve
Suggest changes
Post a comment
Like Article
Like
Save
Share
Report

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
Recommended Problem

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("%d", findSum(A, N));
    return 0;
}


Java




// Java program to find sum of array
// elements using recursion.
  
public 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));
    }
}


Python3




# 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 N <= 0:
        return 0
    else:
        return _findSum(arr, N - 1) + arr[N - 1]
  
# 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


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.


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
?>


Javascript




<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

15

Time Complexity; O(n)

Here n is the number of elements in the array.

Auxiliary Space: O(n)

The extra space is used in recursion call stack.

How does above recursive solution work? 
 

This article is contributed by
Prakhar Agrawal.



Previous Article
Next Article

Similar Reads

Why is Tail Recursion optimization faster than normal Recursion?
What is tail recursion? Tail recursion is defined as a recursive function in which the recursive call is the last statement that is executed by the function. So basically nothing is left to execute after the recursion call. What is non-tail recursion? Non-tail or head recursion is defined as a recursive function in which the recursive call is the f
4 min read
Sum of even elements of an Array using Recursion
Given an array arr[] of integers, the task is to find the sum of even elements from the array. Examples: Input: arr[] = {1, 2, 3, 4, 5, 6, 7, 8} Output: 20 2 + 4 + 6 + 8 = 20Input: arr[] = {4, 1, 3, 6} Output: 10 4 + 6 = 10 Approach: Write a recursive function that takes the array as an argument with the sum variable to store the sum and the index
5 min read
Tail recursion to calculate sum of array elements.
Given an array A[], we need to find the sum of its elements using Tail Recursion Method. We generally want to achieve tail recursion (a recursive function where recursive call is the last thing that function does) so that compilers can optimize the code. Basically, if recursive call is the last statement, the compiler does not need to save the stat
3 min read
Sum of natural numbers using recursion
Given a number n, find sum of first n natural numbers. To calculate the sum, we will use a recursive function recur_sum().Examples : Input : 3 Output : 6 Explanation : 1 + 2 + 3 = 6 Input : 5 Output : 15 Explanation : 1 + 2 + 3 + 4 + 5 = 15 Below is code to find the sum of natural numbers up to n using recursion : C/C++ Code // C++ program to find
3 min read
Sum of the series 1^1 + 2^2 + 3^3 + ..... + n^n using recursion
Given an integer n, the task is to find the sum of the series 11 + 22 + 33 + ..... + nn using recursion. Examples: Input: n = 2 Output: 5 11 + 22 = 1 + 4 = 5 Input: n = 3 Output: 32 11 + 22 + 33 = 1 + 4 + 27 = 32 Approach: Starting from n, start adding all the terms of the series one by one with the value of n getting decremented by 1 in each recur
3 min read
Find Maximum Level Sum in Binary Tree using Recursion
Given a Binary Tree having positive and negative nodes, the task is to find the maximum sum level in it and print the maximum sum.Examples: Input: 4 / \ 2 -5 / \ / \ -1 3 -2 6 Output: 6 Sum of all nodes of the 1st level is 4. Sum of all nodes of the 2nd level is -3. Sum of all nodes of the 3rd level is 6. Hence, the maximum sum is 6. Input: 1 / \ 2
10 min read
Count of subsets with sum equal to X using Recursion
Given an array arr[] of length N and an integer X, the task is to find the number of subsets with a sum equal to X using recursion.Examples: Input: arr[] = {2, 3, 5, 6, 8, 10}, X = 10 Output: 3 Explanation: All possible subsets with sum 10 are {2, 3, 5}, {2, 8}, {10} Input: arr[] = {1, 2, 3, 4, 5}, X = 7 Output: 3 Explanation: All possible subsets
8 min read
Find geometric sum of the series using recursion
Given an integer N, we need to find the geometric sum of the following series using recursion. 1 + 1/3 + 1/9 + 1/27 + ... + 1/(3^n) Examples: Input N = 5 Output: 1.49794 Input: N = 7 Output: 1.49977 Approach:In the above-mentioned problem, we are asked to use recursion. We will calculate the last term and call recursion on the remaining n-1 terms e
3 min read
Sum of N-terms of geometric progression for larger values of N | Set 2 (Using recursion)
A Geometric series is a series with a constant ratio between successive terms. The first term of the series is denoted by a and the common ratio is denoted by r. The series looks like this:- [Tex]a, ar, ar^2, ar^3, ar^4,... [/Tex]The task is to find the sum of such a series, mod M. Examples: Input: a = 1, r = 2, N = 10000, M = 10000 Output: 8751 In
7 min read
Sum of digit of a number using recursion
Given a number, we need to find sum of its digits using recursion.Examples:   Input : 12345Output : 15Input : 45632Output :20   The step-by-step process for a better understanding of how the algorithm works. Let the number be 12345. Step 1-&gt; 12345 % 10 which is equal-too 5 + ( send 12345/10 to next step ) Step 2-&gt; 1234 % 10 which is equal-too
4 min read
Practice Tags :
three90RightbarBannerImg