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 a recursive approach. Towers of Hanoi (TOH), Inorder /Preorder/Postorder Tree Traversals, DFS of Graph, and other analogous issues are examples.
Given a number the task is to check whether the given number is even number or odd number using recursive approach in Python.
Examples:
Example1:
Input:
Enter some random number = 215
Output:
The given number 215 is odd
Example2:
Input:
Enter some random number = 628
Output:
The given number 628 is even
Program to Determine Whether a Given Number is Even or Odd Recursively
Below are the ways to check whether the given number is even or odd recursively :
1)Using Recursion(Static Input)
Approach:
- Give the number as static input.
- Pass the number to a recursive function as an argument.
- Define the base condition as an integer less than two.
- Otherwise, use the number -2 to invoke the function recursively.
- Then return the result and determine whether the number is even or odd.
- The final result should be printed.
- Exit of program.
Below is the implementation:
# function which returns true if the given number # is evennum or oddnum using recursoive approach def checkPrimeRecursion(numb): # Defining the base condition as an integer less than two. if (numb < 2): # Then return the result and determine whether the number is even or odd. return (numb % 2 == 0) # Otherwise, use the number -2 to invoke the function recursively. return (checkPrimeRecursion(numb - 2)) # Give the number as static input. numb = 729 # passing the given number to checkPrimeRecursion # if the returned value is true then it is even number if(checkPrimeRecursion(numb)): print("The given number", numb, "is even") # if the returned value is false then it is odd number else: print("The given number", numb, "is odd")
Output:
The given number 729 is odd
Explanation:
- User must give the number as static input and store it in a variable.
- A recursive function is given the number as an argument.
- The basic requirement is that the number be less than two.
- Otherwise, the function is called recursively with a number less than two.
- The outcome is returned, and an if statement is used to determine whether the integer is odd or even.
- The final result is printed.
2)Using Recursion(User Input)
Approach:
- Enter some random number as user input using int(input()) function.
- Pass the number to a recursive function as an argument.
- Define the base condition as an integer less than two.
- Otherwise, use the number -2 to invoke the function recursively.
- Then return the result and determine whether the number is even or odd.
- The final result should be printed.
- Exit of program.
Below is the implementation:
# function which returns true if the given number # is evennum or oddnum using recursoive approach def checkPrimeRecursion(numb): # Defining the base condition as an integer less than two. if (numb < 2): # Then return the result and determine whether the number is even or odd. return (numb % 2 == 0) # Otherwise, use the number -2 to invoke the function recursively. return (checkPrimeRecursion(numb - 2)) # Give the number as static input. numb = int(input('Enter some random number = ')) # passing the given number to checkPrimeRecursion # if the returned value is true then it is even number if(checkPrimeRecursion(numb)): print("The given number", numb, "is even") # if the returned value is false then it is odd number else: print("The given number", numb, "is odd")
Output:
Enter some random number = 215 The given number 215 is odd
Related Programs:
- Python Program to Check Whether a String is a Palindrome or not Using Recursion
- Python Program to Find the Product of two Numbers Using Recursion
- Python Program to Find if a Number is Prime or Not Prime Using Recursion
- Python Program to Find the Fibonacci Series Using Recursion
- Python Program to Find the Power of a Number Using Recursion
- Python Program to Check Armstrong Number
- Write a Program to Reverse a Number in Python | Reverse Digits or Integers