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 = 20



Input: 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 of the element that is under consideration. If the current element at the required index is even then added it to the sum else do no update the sum and again call the same method for the next index. The termination condition will be when there is no element left to consider i.e. the passed index is out of the bounds of the given array, print the sum and return in that case.

Below is the implementation of the above approach:

C++

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ implementation of the approach
#include <iostream>
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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:

20


My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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 Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.





Article Tags :
Practice Tags :


6


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.