{"id":9561,"date":"2021-09-30T17:30:13","date_gmt":"2021-09-30T12:00:13","guid":{"rendered":"https:\/\/python-programs.com\/?p=9561"},"modified":"2021-11-22T18:33:33","modified_gmt":"2021-11-22T13:03:33","slug":"python-program-to-determine-whether-a-given-number-is-even-or-odd-recursively","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-determine-whether-a-given-number-is-even-or-odd-recursively\/","title":{"rendered":"Python Program to Determine Whether a Given Number is Even or Odd Recursively"},"content":{"rendered":"
Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples<\/a><\/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 Given a number the task is to check whether the given number is even number or odd number using recursive approach in Python.<\/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 check whether the given number is even or odd recursively :<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Are you new to the java programming language? We recommend you to ace up your practice session with these Basic Java Programs Examples 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 …<\/p>\nEnter some random number = 215<\/pre>\n
The given number 215 is odd<\/pre>\n
Enter some random number = 628<\/pre>\n
The given number 628 is even<\/pre>\n
Program to Determine Whether a Given Number is Even or Odd Recursively<\/h2>\n
\n
1)Using Recursion(Static Input)<\/h3>\n
\n
# function which returns true if the given number\r\n# is evennum or oddnum using recursoive approach\r\n\r\n\r\ndef checkPrimeRecursion(numb):\r\n # Defining the base condition as an integer less than two.\r\n if (numb < 2):\r\n # Then return the result and determine whether the number is even or odd.\r\n return (numb % 2 == 0)\r\n # Otherwise, use the number -2 to invoke the function recursively.\r\n return (checkPrimeRecursion(numb - 2))\r\n\r\n\r\n# Give the number as static input.\r\nnumb = 729\r\n# passing the given number to checkPrimeRecursion\r\n# if the returned value is true then it is even number\r\nif(checkPrimeRecursion(numb)):\r\n print(\"The given number\", numb, \"is even\")\r\n# if the returned value is false then it is odd number\r\nelse:\r\n print(\"The given number\", numb, \"is odd\")\r\n<\/pre>\n
The given number 729 is odd<\/pre>\n
\n
2)Using Recursion(User Input)<\/h3>\n
\n
# function which returns true if the given number\r\n# is evennum or oddnum using recursoive approach\r\n\r\n\r\ndef checkPrimeRecursion(numb):\r\n # Defining the base condition as an integer less than two.\r\n if (numb < 2):\r\n # Then return the result and determine whether the number is even or odd.\r\n return (numb % 2 == 0)\r\n # Otherwise, use the number -2 to invoke the function recursively.\r\n return (checkPrimeRecursion(numb - 2))\r\n\r\n\r\n# Give the number as static input.\r\nnumb = int(input('Enter some random number = '))\r\n# passing the given number to checkPrimeRecursion\r\n# if the returned value is true then it is even number\r\nif(checkPrimeRecursion(numb)):\r\n print(\"The given number\", numb, \"is even\")\r\n# if the returned value is false then it is odd number\r\nelse:\r\n print(\"The given number\", numb, \"is odd\")\r\n<\/pre>\n
Enter some random number = 215\r\nThe given number 215 is odd<\/pre>\n
\n
\n