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

PythonServer Side ProgrammingProgramming

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.


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:"))
   print("The number is even")
   print("The number is odd!")


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


  • 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.
Published on 12-Mar-2021 17:32:23