- Related Questions & Answers
- Java program to find whether given number is even or odd
- 8085 program to check whether the given number is even or odd
- Check whether given floating point number is even or odd in Python
- C++ Program to Check Whether Number is Even or Odd
- Python program to determine whether the given number is a Harshad Number
- How to determine if a number is odd or even in JavaScript?
- Check whether the length of given linked list is Even or Odd in Python
- How to Check if a Number is Odd or Even using Python?
- Program to check whether given number is Narcissistic number or not in Python
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- Check whether product of 'n' numbers is even or odd in Python
- Write a Golang program to check whether a given number is prime number or not
- PHP program to check if the total number of divisors of a number is even or odd
- Python program to check whether a given string is Heterogram or not
- Check whether the given number is Euclid Number or not in Python

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

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 −

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!")

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.

Advertisements