Sum of even elements of an Array using Recursion
Last Updated :
19 Dec, 2022
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 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++
// 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
JavaScript
<script>
// Java script implementation of the approach
// Recursive function to find the sum of
// even elements from the array
function 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>
Time Complexity: O(n), where n is the size of the given array.
Auxiliary Space: O(n), due to recursive call stacks.
Similar Reads
Sum of array elements using recursion Given an array of integers, find sum of array elements using recursion. Examples: Input: arr = [1, 2, 3]Output: 6Explanation: 1 + 2 + 3 = 6Input: arr = [15, 12, 13, 10]Output: 50Explanation: 15 + 12 + 13 + 10 = 50We have discussed iterative solution in this Post Sum of elements in a given array. In
2 min read
Sum of elements from an array having even parity Given an array arr[], the task is to calculate the sum of the elements from the given array which has even parity i.e. the number of set bits is even using bitwise operator. Examples: Input: arr[] = {2, 4, 3, 5, 9} Output: 17 Only 3(0011), 5(0101) and 9(1001) have even parity So 3 + 5 + 9 = 17 Input
6 min read
Sum of elements from an array having even parity Given an array arr[], the task is to calculate the sum of the elements from the given array which has even parity i.e. the number of set bits is even using bitwise operator. Examples: Input: arr[] = {2, 4, 3, 5, 9} Output: 17 Only 3(0011), 5(0101) and 9(1001) have even parity So 3 + 5 + 9 = 17 Input
6 min read
Sum of all even occurring element in an array Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are
12 min read
Sum of all even occurring element in an array Given an array of integers containing duplicate elements. The task is to find the sum of all even occurring elements in the given array. That is the sum of all such elements whose frequency is even in the array. Examples: Input : arr[] = {1, 1, 2, 2, 3, 3, 3}Output : 6The even occurring element are
12 min read
Program to print Sum of even and odd elements in an array Prerequisite - Array Basics Given an array, write a program to find the sum of values of even and odd index positions separately. Examples: Input : arr[] = {1, 2, 3, 4, 5, 6} Output :Even index positions sum 9 Odd index positions sum 12 Explanation: Here, n = 6 so there will be 3 even index position
13 min read