In the previous article, we have discussed Python Program to Check Armstrong Number using Recursion
Given a list and the task is to find the sum of odd 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 = [6, 12, 4, 2, 3, 9, 1, 5]
Output:
The Sum of Odd Elements in a given list [6, 12, 4, 2, 3, 9, 1, 5] = 18
Example2:
Input:
Given List = [4, 3, 1, 5, 11]
Output:
The Sum of Odd Elements in a given list [4, 3, 1, 5, 11] = 20
Program to Find Sum of Odd Numbers Using Recursion in a List/Array in Python
Below are the ways to find the sum of odd 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 oddelemt_sum function.
- Create a recursive function to say oddelemt_sum which takes the given list and length of the given list as the arguments and returns the sum of odd 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 odd using the 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 oddelemt_sum function.{Recursive Logic}
- Return rslt_sum.
- Print the sum of odd numbers in the above-given list.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say oddelemt_sum which takes the given list and length # of the given list as the arguments and returns the sum of odd numbers in a given list # using recursion. def oddelemt_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 odd 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 oddelemt_sum function # {Recursive Logic}. oddelemt_sum(gven_lst, k) # Return rslt_sum. return rslt_sum # Give the list as static input and store it in a variable. gven_lst = [6, 12, 4, 2, 3, 9, 1, 5] # 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 oddelemt_sum # function. # Print the sum of odd numbers in the above-given list. print("The Sum of Odd Elements in a given list", gven_lst, "=", oddelemt_sum(gven_lst, len_lst))
Output:
The Sum of Odd Elements in a given list [6, 12, 4, 2, 3, 9, 1, 5] = 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 oddelemt_sum function.
- Create a recursive function to say oddelemt_sum which takes the given list and length of the given list as the arguments and returns the sum of odd 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 odd using the 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 oddelemt_sum function.{Recursive Logic}
- Return rslt_sum.
- Print the sum of odd numbers in the above-given list.
- The Exit of the Program.
Below is the implementation:
# Create a recursive function to say oddelemt_sum which takes the given list and length # of the given list as the arguments and returns the sum of odd numbers in a given list # using recursion. def oddelemt_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 odd 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 oddelemt_sum function # {Recursive Logic}. oddelemt_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 oddelemt_sum # function. # Print the sum of odd numbers in the above-given list. print("The Sum of Odd Elements in a given list", gven_lst, "=", oddelemt_sum(gven_lst, len_lst))
Output:
Enter some random List Elements separated by spaces = 4 3 1 5 11 The Sum of Odd Elements in a given list [4, 3, 1, 5, 11] = 20
Find the best practical and ready-to-use Python Programming Examples that you can simply run on a variety of platforms and never stop learning.