{"id":20432,"date":"2021-09-12T12:01:53","date_gmt":"2021-09-12T06:31:53","guid":{"rendered":"https:\/\/python-programs.com\/?p=20432"},"modified":"2021-11-22T18:36:21","modified_gmt":"2021-11-22T13:06:21","slug":"python-program-to-find-sum-of-odd-numbers-using-recursion-in-a-list-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-odd-numbers-using-recursion-in-a-list-array\/","title":{"rendered":"Python Program to Find Sum of Odd Numbers Using Recursion in a List\/Array"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Check Armstrong Number using Recursion<\/a><\/p>\n Given a list and the task is to find the sum of odd numbers using recursion in a given list in python.<\/p>\n Recursion:<\/strong><\/p>\n 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.<\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Below are the ways to find the sum of odd numbers using recursion in a given list in python:<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Find the best practical and ready-to-use Python Programming Examples<\/a> that you can simply run on a variety of platforms and never stop learning.<\/p>\n 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 …<\/p>\nGiven List = [6, 12, 4, 2, 3, 9, 1, 5]<\/pre>\n
The Sum of Odd Elements in a given list [6, 12, 4, 2, 3, 9, 1, 5] = 18<\/pre>\n
Given List = [4, 3, 1, 5, 11]<\/pre>\n
The Sum of Odd Elements in a given list [4, 3, 1, 5, 11] = 20<\/pre>\n
Program to Find Sum of Odd Numbers Using Recursion in a List\/Array in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say oddelemt_sum which takes the given list and length\r\n# of the given list as the arguments and returns the sum of odd numbers in a given list\r\n# using recursion.\r\n\r\n\r\ndef oddelemt_sum(gven_lst, len_lst):\r\n # Make the rslt_sum a global declaration.\r\n global rslt_sum\r\n # Check if the length of the given list is greater than 0 using the if conditional\r\n # statement.\r\n if(len_lst > 0):\r\n # If the statement is true, then subtract 1 from the length of the given list and\r\n # store it in a variable k.\r\n k = len_lst-1\r\n # Check if the element present at the index k of the given list is odd using modulus\r\n # operator and if conditional statement.\r\n if(gven_lst[k] % 2 != 0):\r\n # If the statement is true, add the element present at the index k of the\r\n # given list to the above-initialized rslt_sum.\r\n # Store it in the same variable.\r\n rslt_sum = rslt_sum+gven_lst[k]\r\n # Pass the given list and k value as the arguments to the oddelemt_sum function\r\n # {Recursive Logic}.\r\n oddelemt_sum(gven_lst, k)\r\n # Return rslt_sum.\r\n return rslt_sum\r\n\r\n\r\n# Give the list as static input and store it in a variable.\r\ngven_lst = [6, 12, 4, 2, 3, 9, 1, 5]\r\n# Calculate the length of the given list and store it in another variable.\r\nlen_lst = len(gven_lst)\r\n# Take a variable say rslt_sum and initialize its value to 0.\r\nrslt_sum = 0\r\n# Pass the given list and length of the given list as the arguments to the oddelemt_sum\r\n# function.\r\n# Print the sum of odd numbers in the above-given list.\r\nprint(\"The Sum of Odd Elements in a given list\",\r\n gven_lst, \"=\", oddelemt_sum(gven_lst, len_lst))\r\n<\/pre>\n
The Sum of Odd Elements in a given list [6, 12, 4, 2, 3, 9, 1, 5] = 18<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say oddelemt_sum which takes the given list and length\r\n# of the given list as the arguments and returns the sum of odd numbers in a given list\r\n# using recursion.\r\n\r\n\r\ndef oddelemt_sum(gven_lst, len_lst):\r\n # Make the rslt_sum a global declaration.\r\n global rslt_sum\r\n # Check if the length of the given list is greater than 0 using the if conditional\r\n # statement.\r\n if(len_lst > 0):\r\n # If the statement is true, then subtract 1 from the length of the given list and\r\n # store it in a variable k.\r\n k = len_lst-1\r\n # Check if the element present at the index k of the given list is odd using modulus\r\n # operator and if conditional statement.\r\n if(gven_lst[k] % 2 != 0):\r\n # If the statement is true, add the element present at the index k of the\r\n # given list to the above-initialized rslt_sum.\r\n # Store it in the same variable.\r\n rslt_sum = rslt_sum+gven_lst[k]\r\n # Pass the given list and k value as the arguments to the oddelemt_sum function\r\n # {Recursive Logic}.\r\n oddelemt_sum(gven_lst, k)\r\n # Return rslt_sum.\r\n return rslt_sum\r\n\r\n\r\n# Give the list as user input using list(),map(),input(),and split() functions.\r\n# Store it in a variable.\r\ngven_lst = list(map(int, input(\r\n 'Enter some random List Elements separated by spaces = ').split()))\r\n# Calculate the length of the given list and store it in another variable.\r\nlen_lst = len(gven_lst)\r\n# Take a variable say rslt_sum and initialize its value to 0.\r\nrslt_sum = 0\r\n# Pass the given list and length of the given list as the arguments to the oddelemt_sum\r\n# function.\r\n# Print the sum of odd numbers in the above-given list.\r\nprint(\"The Sum of Odd Elements in a given list\",\r\n gven_lst, \"=\", oddelemt_sum(gven_lst, len_lst))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 4 3 1 5 11\r\nThe Sum of Odd Elements in a given list [4, 3, 1, 5, 11] = 20<\/pre>\n
\n