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 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++
// 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; } |
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
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.