Check Even or Odd Numbers using recursion in Python

We use the concept of getting the remainder without using the modulus operator by subtracting the number by 2. In the below code, we are calling the recursion function by subtracting the number by 2. In the base condition, we are checking whether the n becomes equal to 1 or 0. If n==1 it is odd and if n==0 number is even.

Python3




# defining the function having the one parameter as input
def evenOdd(n):
     
    # if remainder is 0 then num is even
    if(n==0):
        return True
       
    # if remainder is 1 then num is odd
    elif(n==1):
        return False
    else:
        return evenOdd(n-2)
       
num=33
 
if(evenOdd(num)):
    print(num,"num is even")
else:
    print(num,"num is odd")


Output

33 num is odd

Time Complexity: O(n/2)
Auxiliary Space: O(1)

Python Program to Check if a Number is Odd or Even

Given a number and our task is to check number is Even or Odd using Python. Those numbers which are completely divisible by 2 or give 0 as the remainder are known as even number and those that are not or gives a remainder other than 0 are known as odd numbers.

Example:

Input: 2
Output: Even number

Input: 41
Output: Odd Number

Similar Reads

Check Even or Odd Number using the modulo operator in Python

In this method, we use the modulo operator (%) to check if the number is even or odd in Python. The Modulo operator returns the remainder when divided by any number so, we will evaluate the x%2, and if the result is 0 a number is even otherwise it is an odd number....

Check Even or Odd Numbers using recursion in Python

...

Check Even or Odd Numbers using & operator in Python

We use the concept of getting the remainder without using the modulus operator by subtracting the number by 2. In the below code, we are calling the recursion function by subtracting the number by 2. In the base condition, we are checking whether the n becomes equal to 1 or 0. If n==1 it is odd and if n==0 number is even....