Program to Find Sum of Even Numbers Using Recursion in a ListArray

Python Program to Find Sum of Even Numbers Using Recursion in a List/Array

In the previous article, we have discussed Python Program to Find Maximum and Minimum Elements in List/Array Using Recursion

Given a list and the task is to find the sum of even numbers using recursion in a given list in python.

Recursion:

Recursion is the process by which a function calls itself directly or indirectly, and the associated function is known as a recursive function. Certain issues can be addressed fairly easily using a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.

Examples:

Example1:

Input:

Given List = [1, 6, 3, 7, 8, 4]

Output:

The Sum of Even Elements in a given list [1, 6, 3, 7, 8, 4] = 18

Example2:

Input:

Given List = [14, 10, 2, 5, 8, 1, 4]

Output:

The Sum of Even Elements in a given list [14, 10, 2, 5, 8, 1, 4] = 38

Program to Find Sum of Even Numbers Using Recursion in a List/Array in Python

Below are the ways to find the sum of even numbers using recursion in a given list in python:

Method #1: Using Recursion (Static Input)

Approach:

  • Give the list as static input and store it in a variable.
  • Calculate the length of the given list and store it in another variable.
  • Take a variable say rslt_sum and initialize its value to 0.
  • Pass the given list and length of the given list as the arguments to the evenelemt_sum function.
  • Create a recursive function to say evenelemt_sum which takes the given list and length of the given list as the arguments and returns the sum of even numbers in a given list using recursion.
  • Make the rslt_sum a global declaration.
  • Check if the length of the given list is greater than 0 using the if conditional statement.
  • If the statement is true, then subtract 1 from the length of the given list and store it in a variable k.
  • Check if the element present at the index k of the given list is even using modulus operator and if conditional statement.
  • If the statement is true, add the element present at the index k of the given list to the above-initialized rslt_sum.
  • Store it in the same variable.
  • Pass the given list and k value as the arguments to the evenelemt_sum function.{Recursive Logic}
  • Return rslt_sum.
  • Print the sum of even numbers in the above-given list.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say evenelemt_sum which takes the given list and length
# of the given list as the arguments and returns the sum of even numbers in a given list
# using recursion.


def evenelemt_sum(gven_lst, len_lst):
    # Make the rslt_sum a global declaration.
    global rslt_sum
    # Check if the length of the given list is greater than 0 using the if conditional
    # statement.
    if(len_lst > 0):
        # If the statement is true, then subtract 1 from the length of the given list and
        # store it in a variable k.
        k = len_lst-1
   # Check if the element present at the index k of the given list is even using modulus
   # operator and if conditional statement.
        if(gven_lst[k] % 2 == 0):
            # If the statement is true, add the element present at the index k of the
            # given list to the above-initialized rslt_sum.
            # Store it in the same variable.
            rslt_sum = rslt_sum+gven_lst[k]
           # Pass the given list and k value as the arguments to the evenelemt_sum function
           # {Recursive Logic}.
        evenelemt_sum(gven_lst, k)
       # Return rslt_sum.
    return rslt_sum


# Give the list as static input and store it in a variable.
gven_lst = [1, 6, 3, 7, 8, 4]
# Calculate the length of the given list and store it in another variable.
len_lst = len(gven_lst)
# Take a variable say rslt_sum and initialize its value to 0.
rslt_sum = 0
# Pass the given list and length of the given list as the arguments to the evenelemt_sum
# function.
# Print the sum of even numbers in the above-given list.
print("The Sum of Even Elements in a given list",
      gven_lst, "=", evenelemt_sum(gven_lst, len_lst))

Output:

The Sum of Even Elements in a given list [1, 6, 3, 7, 8, 4] = 18

Method #2: Using Recursion (User Input)

Approach:

  • Give the list as user input using list(),map(),input(),and split() functions.
  • Store it in a variable.
  • Calculate the length of the given list and store it in another variable.
  • Take a variable say rslt_sum and initialize its value to 0.
  • Pass the given list and length of the given list as the arguments to the evenelemt_sum function.
  • Create a recursive function to say evenelemt_sum which takes the given list and length of the given list as the arguments and returns the sum of even numbers in a given list using recursion.
  • Make the rslt_sum a global declaration.
  • Check if the length of the given list is greater than 0 using the if conditional statement.
  • If the statement is true, then subtract 1 from the length of the given list and store it in a variable k.
  • Check if the element present at the index k of the given list is even using modulus operator and if conditional statement.
  • If the statement is true, add the element present at the index k of the given list to the above-initialized rslt_sum.
  • Store it in the same variable.
  • Pass the given list and k value as the arguments to the evenelemt_sum function.{Recursive Logic}
  • Return rslt_sum.
  • Print the sum of even numbers in the above-given list.
  • The Exit of the Program.

Below is the implementation:

# Create a recursive function to say evenelemt_sum which takes the given list and length
# of the given list as the arguments and returns the sum of even numbers in a given list
# using recursion.


def evenelemt_sum(gven_lst, len_lst):
    # Make the rslt_sum a global declaration.
    global rslt_sum
    # Check if the length of the given list is greater than 0 using the if conditional
    # statement.
    if(len_lst > 0):
        # If the statement is true, then subtract 1 from the length of the given list and
        # store it in a variable k.
        k = len_lst-1
   # Check if the element present at the index k of the given list is even using modulus
   # operator and if conditional statement.
        if(gven_lst[k] % 2 == 0):
            # If the statement is true, add the element present at the index k of the
            # given list to the above-initialized rslt_sum.
            # Store it in the same variable.
            rslt_sum = rslt_sum+gven_lst[k]
           # Pass the given list and k value as the arguments to the evenelemt_sum function
           # {Recursive Logic}.
        evenelemt_sum(gven_lst, k)
       # Return rslt_sum.
    return rslt_sum


# Give the list as user input using list(),map(),input(),and split() functions.
# Store it in a variable.
gven_lst = list(map(int, input(
   'Enter some random List Elements separated by spaces = ').split()))
# Calculate the length of the given list and store it in another variable.
len_lst = len(gven_lst)
# Take a variable say rslt_sum and initialize its value to 0.
rslt_sum = 0
# Pass the given list and length of the given list as the arguments to the evenelemt_sum
# function.
# Print the sum of even numbers in the above-given list.
print("The Sum of Even Elements in a given list",
      gven_lst, "=", evenelemt_sum(gven_lst, len_lst))

Output:

Enter some random List Elements separated by spaces = 14 10 2 5 8 1 4
The Sum of Even Elements in a given list [14, 10, 2, 5, 8, 1, 4] = 38

Enhance your coding skills with our list of Python Basic Programs provided and become a pro in the general-purpose programming language Python in no time.