# Python Program to Determine Whether a Given Number is Even or Odd Recursively

PythonServer Side ProgrammingProgramming

#### Beyond Basic Programming - Intermediate Python

Most Popular

36 Lectures 3 hours

#### Practical Machine Learning using Python

Best Seller

91 Lectures 23.5 hours

#### Practical Data Science using Python

22 Lectures 6 hours

When it is required to check if a given number is an odd number or an even number using recursion, recursion can be used.

The recursion computes output of small bits of the bigger problem, and combines these bits to give the solution to the bigger problem.

## Example

Below is a demonstration for the same −

Live Demo

def check_odd_even(my_num):
if (my_num < 2):
return (my_num % 2 == 0)
return (check_odd_even(my_num - 2))
my_number = int(input("Enter the number that needs to be checked:"))
if(check_odd_even(my_number)==True):
print("The number is even")
else:
print("The number is odd!")

## Output

Enter the number that needs to be checked:48
The number is even

## Explanation

• A method named ‘check_odd_even’ is defined, that takes a number as parameter.
• If the number is less than 2, the remainder of the number when divided by 2 is computed, and checked wih 0.
• The function is called again, and this time, the parameter passed is the number decremented by 2.
• Outside the function, a number is taken as input by the user.
• The function is called, and checked to see if it is ‘True’, if yes, it is determined as an even number.
• Else it is considered an odd number.
• It is returned as output.
Updated on 12-Mar-2021 13:03:21