Sum of even elements of an Array using Recursion - GeeksforGeeks

27 Apr, 2021
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 of the element that is under consideration. If the current element at the required index is even then added to the sum else do not 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++
Java
Python3
C#
Javascript
// C++ implementation of the approach#include <iostream>using namespace std; // Recursive function to find the sum of// even elements from the arrayvoid 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 codeint 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 implementation of the approachimport java.util.*;import java.lang.*;import java.io.*; class GFG{ // Recursive function to find the sum of// even elements from the arraystatic 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 codepublic 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 implementation of the approach # Recursive function to find the sum of# even elements from the arraydef 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 codeif __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# implementation of the approachusing System;     class GFG{ // Recursive function to find the sum of// even elements from the arraystatic 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 codepublic 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
<script>// Java script implementation of the approach  // Recursive function to find the sum of// even elements from the arrayfunction 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        document.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     let arr = [ 1, 2, 3, 4, 5, 6, 7, 8 ];    let n = arr.length;    let sum = 0;     SumOfEven(arr, n - 1, sum);    //contributed by bobby    </script>
20
 
nidhiva
Rajput-Ji
princiraj1992
gottumukkalabobby
Arrays
Recursion
Arrays
Recursion
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Window Sliding Technique
Trapping Rain Water
Building Heap from Array
Program to find sum of elements in a given array
Reversal algorithm for array rotation
Write a program to print all permutations of a given string
Program for Tower of Hanoi
Recursion
Program for Sum of the digits of a given number
Backtracking | Introduction