Skip to content
Related Articles

Related Articles

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

Improve Article
Save Article
  • Difficulty Level : Basic
  • Last Updated : 13 Jun, 2022
Improve Article
Save Article

In this article, We will see how to write the program to find the given number is Even or Odd Using the Recursion Method. If the number is even returned true else false in Python.

For that, we use the Operator Called Modulus Operator. This operator used in the operation when we need to calculate the remainder of that number when divided by any divisor.

  • Even Number: A number that is completely divisible by 2 Hence remainders is 0
  • Odd Number: A number that is not divisible by 2 Hence remainders is 1

Examples:

Input: 2
Output: True
Explanation : 2 % 2==0 So True

Input: 5
Output: False 
Explanation : 2 % 2 != 0 So, False

Methods #1:

Approach :

  • We use the concept of getting the remainder without using the modulus operator by subtracting the number by number-2
  • If at last, we get any remainder then that number is odd and return the False for that number
  • Else the number is even and return True for that number

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)
       
# Input by geeks
num=3
if(evenOdd(num)):
    print(num,"num is even")
else:
    print(num,"num is odd")


Output

3 num is odd

Methods #2:

We use the modulus operator to find the even or odd Like if num % 2 == 0 then the remainder is 0 so the given number is even and return True 

Else if num % 2 != 0 then the remainder is not zero so the given number is odd and return False

Python3




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


Output

3 num is odd

Methods #3: Using the BINARY AND (&) operator.

Approach: For this method, we just need to use the & operator, which is a bitwise operator, and it works the same way as the % operator for this particular case. 

If given number & 1 gives 1, the number would be odd, and even otherwise.

Python3




# defining the function having
# the one parameter as input
 
def evenOdd(n):
 
  # if n&1 == 0, then num is even
  if n & 1:
    return False
  # if n&1 == 1, then num is odd
  else:
    return True
 
 
# Input by geeks
num = 3
if(evenOdd(num)):
    print(num, "num is even")
else:
    print(num, "num is odd")


Output

3 num is odd

Methods #4: To print all the even numbers for the range (a, b)

Here We will print all the even numbers for the Given Range n in the Function evenOdd(n)

Python3




# writing the function having the range
# and printing the even in that range
def evenOdd(a,b):
    if(a>b):
        return
    print(a ,end=" ")
    return evenOdd(a+2,b)
 
# input by geeks
x=2
y=10
if(x % 2 ==0):
    x=x
     
else:
     
    # if the number x is odd then
    # add 1 the number into it to
    # avoid any error to make it even
    x+=1
evenOdd(x,y)


Output

2 4 6 8 10 

My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!