Find sum of even numbers using recursion in an array

Problem statement:- Program to Find the sum of even numbers using recursion in an array.

Data requirement:-

   Input Data:-n, arr

  Output Data:-
SumOfEvenElement(arr,n)

Program in C

Here is the source code of the C Program to Find the sum of even numbers using recursion in an array.

Code:

#include<stdio.h>
int SumOfEvenElement(int arr[], int n)
{
    static int sum=0;
    int i;
    if(n>0)
    {
        i=n-1;
        if(arr[i]%2==0)
        sum=sum+arr[i];
        SumOfEvenElement(arr,i);
    }
return sum;
}
int main()
{
    int n,j;
    printf("Enter your array size:");
    scanf("%d",&n);
    int arr[n];
    printf("Enter the Array Element:");
    for(j=0;j<n;j++)
    {
        scanf("%d",&arr[j]);
    }
    printf("Sum of even Element is: %d",SumOfEvenElement(arr,n));
}

Input/Output:
Enter your array size:5
Enter the Array Element:4 8 10 3 5
Sum of even Element is: 22

Program in C++

Here is the source code of the C++ Program to Find the sum of even numbers using recursion in an array.

Code:

#include<iostream>
using namespace std;
int SumOfEvenElement(int arr[], int n)
{
    static int sum=0;
    int i;
    if(n>0)
    {
        i=n-1;
        if(arr[i]%2==0)
        sum=sum+arr[i];
        SumOfEvenElement(arr,i);
    }
return sum;
}
int main()
{
    int n,j;
    cout<<"Enter your array size:";
    cin>>n;
    int arr[n];
    cout<<"Enter the Array Element:";
    for(j=0;j<n;j++)
    {
        cin>>arr[j];
    }
    cout<<"Sum of even Element is: "<<SumOfEvenElement(arr,n);
}

Input/Output:
Enter your array size:6
Enter the Array Element:9 13 55 65 100 2
Sum of even Element is: 102

Program in Java

Here is the source code of the Java Program to Find the sum of even numbers using recursion in an array.

Code:

import java.util.Scanner;
public class SumEvenElement {
int sum=0;
int SumOfEvenElement(int arr[], int n)
{
    int i;
    if(n>0)
    {
        i=n-1;
        if(arr[i]%2==0)
        sum=sum+arr[i];
        SumOfEvenElement(arr,i);
    }
return sum;
}
public static void main(String[] args) {
Scanner cs=new Scanner(System.in);
int n,i;
System.out.println("Enter your Array Size:");
n=cs.nextInt();
int arr[]=new int[n];
System.out.println("Enter the Array Element:");
for(i=0;i<n;i++)
{
arr[i]=cs.nextInt();
}
SumEvenElement ob=new SumEvenElement();
System.out.print("Sum of even Element is:"+ob.SumOfEvenElement(arr,n));
cs.close();
}
}

Input/Output:
Enter your Array Size:
8
Enter the Array Element:
4 5 10 13 52 33 3 7
Sum of even Element is:66

Program in Python

Here is the source code of the Python program to Find the sum of even numbers using recursion in an array.

Code:

sum=0
def SumOfEvenElement(arr,n):
    global sum
    if(n>0):
        i=n-1
        if(arr[i]%2==0):
            sum=sum+arr[i]
        SumOfEvenElement(arr,i)
    return sum
arr=[]
n = int(input("Enter the size of the array: "))
print("Enter the Element of the array:")
for i in range(0,n):
    num = int(input())
    arr.append(num)

print("Sum of even Element is:",SumOfEvenElement(arr,n))

Input/Output:

Post a Comment

0 Comments