Suma de elementos de array usando recursividad

Dada una array de enteros, encuentre la suma de los elementos de la array usando la recursividad. 

Ejemplos: 

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;
}

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));
    }
}

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>

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *