📜  使用递归的数组偶数元素之和

📅  最后修改于: 2021-04-27 05:41:31             🧑  作者: Mango

给定一个整数数组arr [] ,任务是从数组中找到偶数元素的和。

例子:

方法:编写一个递归函数,该函数将数组作为带有sum变量的参数来存储要考虑的元素的sum和索引。如果位于所需索引处的当前元素是偶数,然后将其添加到总和中,否则不更新总和,并再次为下一个索引调用相同的方法。终止条件将是当没有元素要考虑时,即所传递的索引超出给定数组的边界,在这种情况下打印总和并返回。

下面是上述方法的实现:

C++
// C++ implementation of the approach
#include 
using namespace std;
  
// Recursive function to find the sum of
// even elements from the array
void SumOfEven(int arr[], int i, int sum)
{
  
    // If current index is invalid i.e. all
    // the elements of the array
    // have been traversed
    if (i < 0) {
  
        // Print the sum
        cout << sum;
        return;
    }
  
    // If current element is even
    if ((arr[i]) % 2 == 0) {
  
        // Add it to the sum
        sum += (arr[i]);
    }
  
    // Recursive call for the next element
    SumOfEven(arr, i - 1, sum);
}
  
// Driver code
int main()
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int n = sizeof(arr) / sizeof(arr[0]);
    int sum = 0;
  
    SumOfEven(arr, n - 1, sum);
  
    return 0;
}


Java
// Java implementation of the approach
import java.util.*;
import java.lang.*;
import java.io.*;
  
class GFG
{
  
// Recursive function to find the sum of
// even elements from the array
static void SumOfEven(int arr[],    
                      int i, int sum)
{
  
    // If current index is invalid i.e. all
    // the elements of the array
    // have been traversed
    if (i < 0) 
    {
  
        // Print the sum
        System.out.print(sum);
        return;
    }
  
    // If current element is even
    if ((arr[i]) % 2 == 0)
    {
  
        // Add it to the sum
        sum += (arr[i]);
    }
  
    // Recursive call for the next element
    SumOfEven(arr, i - 1, sum);
}
  
// Driver code
public static void main (String[] args) 
              throws java.lang.Exception
{
    int arr[] = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int n = arr.length;
    int sum = 0;
  
    SumOfEven(arr, n - 1, sum);
}
}
  
// This code is contributed by nidhiva


Python3
# Python3 implementation of the approach
  
# Recursive function to find the sum of
# even elements from the array
def SumOfEven(arr, i, sum):
  
    # If current index is invalid i.e. 
    # all the elements of the array
    # have been traversed
    if (i < 0):
  
        # Print the sum
        print(sum);
        return;
  
    # If current element is even
    if ((arr[i]) % 2 == 0):
  
        # Add it to the sum
        sum += (arr[i]);
  
    # Recursive call for the next element
    SumOfEven(arr, i - 1, sum);
  
# Driver code
if __name__ == '__main__':
    arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ];
    n = len(arr);
    sum = 0;
  
    SumOfEven(arr, n - 1, sum);
  
# This code is contributed by PrinciRaj1992


C#
// C# implementation of the approach
using System;
      
class GFG
{
  
// Recursive function to find the sum of
// even elements from the array
static void SumOfEven(int []arr, 
                      int i, int sum)
{
  
    // If current index is invalid i.e. all
    // the elements of the array
    // have been traversed
    if (i < 0) 
    {
  
        // Print the sum
        Console.Write(sum);
        return;
    }
  
    // If current element is even
    if ((arr[i]) % 2 == 0)
    {
  
        // Add it to the sum
        sum += (arr[i]);
    }
  
    // Recursive call for the next element
    SumOfEven(arr, i - 1, sum);
}
  
// Driver code
public static void Main (String[] args)
{
    int []arr = { 1, 2, 3, 4, 5, 6, 7, 8 };
    int n = arr.Length;
    int sum = 0;
  
    SumOfEven(arr, n - 1, sum);
}
}
  
// This code is contributed by Rajput-Ji


输出:
20