{"id":20408,"date":"2021-09-11T15:38:20","date_gmt":"2021-09-11T10:08:20","guid":{"rendered":"https:\/\/python-programs.com\/?p=20408"},"modified":"2021-11-22T18:36:21","modified_gmt":"2021-11-22T13:06:21","slug":"python-program-to-find-sum-of-even-numbers-using-recursion-in-a-list-array","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-find-sum-of-even-numbers-using-recursion-in-a-list-array\/","title":{"rendered":"Python Program to Find Sum of Even Numbers Using Recursion in a List\/Array"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Find Maximum and Minimum Elements in List\/Array Using Recursion<\/a><\/p>\n Given a list and the task is to find the sum of even 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 even 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 Enhance your coding skills with our list of Python Basic Programs<\/a> provided and become a pro in the general-purpose programming language Python in no time.<\/p>\n 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, …<\/p>\nGiven List = [1, 6, 3, 7, 8, 4]<\/pre>\n
The Sum of Even Elements in a given list [1, 6, 3, 7, 8, 4] = 18<\/pre>\n
Given List = [14, 10, 2, 5, 8, 1, 4]<\/pre>\n
The Sum of Even Elements in a given list [14, 10, 2, 5, 8, 1, 4] = 38<\/pre>\n
Program to Find Sum of Even 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 evenelemt_sum which takes the given list and length\r\n# of the given list as the arguments and returns the sum of even numbers in a given list\r\n# using recursion.\r\n\r\n\r\ndef evenelemt_sum(gven_lst, len_lst):\r\n # Make the\u00a0rslt_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 even 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\u00a0the 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 evenelemt_sum function\r\n # {Recursive Logic}.\r\n evenelemt_sum(gven_lst, k)\r\n # Return\u00a0rslt_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 = [1, 6, 3, 7, 8, 4]\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 evenelemt_sum\r\n# function.\r\n# Print the sum of even numbers in the above-given list.\r\nprint(\"The Sum of Even Elements in a given list\",\r\n gven_lst, \"=\", evenelemt_sum(gven_lst, len_lst))\r\n<\/pre>\n
The Sum of Even Elements in a given list [1, 6, 3, 7, 8, 4] = 18<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say evenelemt_sum which takes the given list and length\r\n# of the given list as the arguments and returns the sum of even numbers in a given list\r\n# using recursion.\r\n\r\n\r\ndef evenelemt_sum(gven_lst, len_lst):\r\n # Make the\u00a0rslt_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 even 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\u00a0the 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 evenelemt_sum function\r\n # {Recursive Logic}.\r\n evenelemt_sum(gven_lst, k)\r\n # Return\u00a0rslt_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 evenelemt_sum\r\n# function.\r\n# Print the sum of even numbers in the above-given list.\r\nprint(\"The Sum of Even Elements in a given list\",\r\n gven_lst, \"=\", evenelemt_sum(gven_lst, len_lst))\r\n<\/pre>\n
Enter some random List Elements separated by spaces = 14 10 2 5 8 1 4\r\nThe Sum of Even Elements in a given list [14, 10, 2, 5, 8, 1, 4] = 38<\/pre>\n
\n