{"id":20270,"date":"2021-09-09T19:57:41","date_gmt":"2021-09-09T14:27:41","guid":{"rendered":"https:\/\/python-programs.com\/?p=20270"},"modified":"2021-11-22T18:36:24","modified_gmt":"2021-11-22T13:06:24","slug":"python-program-to-print-even-numbers-in-given-range-using-recursion","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-print-even-numbers-in-given-range-using-recursion\/","title":{"rendered":"Python Program to Print Even Numbers in Given Range Using Recursion"},"content":{"rendered":"
In the previous article, we have discussed Python Program to Divide Two Numbers Using Recursion<\/a><\/p>\n Given the Lower and upper limits as range and the task is to print the even numbers in a given range.<\/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 print the even numbers in a given range 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 Remediate your knowledge gap by attempting the Python Code Examples<\/a> regularly and understand the areas of need and work on them.<\/p>\n In the previous article, we have discussed Python Program to Divide Two Numbers Using Recursion Given the Lower and upper limits as range and the task is to print the even numbers in a given range. Recursion: Recursion is the process by which a function calls itself directly or indirectly, and the associated function is …<\/p>\n Given Lower Limit = 30\r\n Given Upper Limit = 50<\/pre>\n
The Even numbers in a given range 30 and 50 are :\r\n30 32 34 36 38 40 42 44 46 48 50<\/pre>\n
Given Lower Limit = 60\r\nGiven Upper Limit = 100<\/pre>\n
The Even numbers in a given range 60 and 100 are :\r\n60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100<\/pre>\n
Program to Print Even Numbers in Given Range Using Recursion in Python<\/h2>\n
\n
Method #1: Using Recursion (Static Input)<\/h3>\n
\n
# Create a recursive function to say even_range which takes the given lower and upper\r\n# limits as arguments and returns the even numbers in a given range using recursion.\r\n\r\n\r\ndef even_range(gvnlowr_lmt, gvnuppr_lmt):\r\n # Check if the given lower limit value is greater than the upper limit using the if\r\n # conditional statement.\r\n if gvnlowr_lmt > gvnuppr_lmt:\r\n # If the statement is true, then return.\r\n return\r\n # After the return statement, print the given lower limit separated by spaces.\r\n print(gvnlowr_lmt, end=\" \")\r\n # Return even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.\r\n return even_range(gvnlowr_lmt+2, gvnuppr_lmt)\r\n\r\n\r\n# Give the lower limit as static input and store it in a variable.\r\ngvnlowr_lmt = 30\r\n# Give the upper limit as static input and store it in another variable.\r\ngvnuppr_lmt = 50\r\n# Pass the given lower and upper limits as the arguments to even_range function.\r\n# Print the even Numbers in a given range.\r\nprint(\"The Even numbers in a given range\",\r\n gvnlowr_lmt, \"and\", gvnuppr_lmt, \"are :\")\r\neven_range(gvnlowr_lmt, gvnuppr_lmt)\r\n<\/pre>\n
The Even numbers in a given range 30 and 50 are :\r\n30 32 34 36 38 40 42 44 46 48 50<\/pre>\n
Method #2: Using Recursion (User Input)<\/h3>\n
\n
# Create a recursive function to say even_range which takes the given lower and upper\r\n# limits as arguments and returns the even numbers in a given range using recursion.\r\n\r\n\r\ndef even_range(gvnlowr_lmt, gvnuppr_lmt):\r\n # Check if the given lower limit value is greater than the upper limit using the if\r\n # conditional statement.\r\n if gvnlowr_lmt > gvnuppr_lmt:\r\n # If the statement is true, then return.\r\n return\r\n # After the return statement, print the given lower limit separated by spaces.\r\n print(gvnlowr_lmt, end=\" \")\r\n # Return even_range(gvnlowr_lmt+2, gvnuppr_lmt) {Recursive Logic}.\r\n return even_range(gvnlowr_lmt+2, gvnuppr_lmt)\r\n\r\n\r\n# Give the lower limit as user input using the int(input()) function\r\n# and store it in a variable.\r\ngvnlowr_lmt = int(input(\"Enter some random number = \"))\r\n# Give the upper limit as user input using the int(input()) function\r\n# and store it in another variable.\r\ngvnuppr_lmt = int(input(\"Enter some random number = \"))\r\n# Pass the given lower and upper limits as the arguments to even_range function.\r\n# Print the even Numbers in a given range.\r\nprint(\"The Even numbers in a given range\",\r\n gvnlowr_lmt, \"and\", gvnuppr_lmt, \"are :\")\r\neven_range(gvnlowr_lmt, gvnuppr_lmt)\r\n<\/pre>\n
Enter some random number = 60\r\nEnter some random number = 100\r\nThe Even numbers in a given range 60 and 100 are :\r\n60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100<\/pre>\n
\n