- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Determine Whether a Given Number is Even or Odd Recursively
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.
- Golang Program to Determine Recursively Whether a Given Number is Even or Odd
- Java program to find whether given number is even or odd
- 8085 program to check whether the given number is even or odd
- Java Program to Check Whether a Number is Even or Odd
- C++ Program to Check Whether Number is Even or Odd
- Check whether given floating point number is even or odd in Python
- Python program to determine whether the given number is a Harshad Number
- 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
- Check whether product of 'n' numbers is even or odd in Python
- Python Program to Determine How Many Times a Given Letter Occurs in a String Recursively
- PHP program to check if the total number of divisors of a number is even or odd
- Write a Golang program to check whether a given number is prime number or not