
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
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.
Example
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!")
Output
Enter the number that needs to be checked:48 The number is even
Explanation
- 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.
- Related Articles
- 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
- Check whether given floating point number is even or odd in Python
- Java Program to Check Whether a Number is Even or Odd
- Haskell Program to Check Whether a Number is Even or Odd
- 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
- PHP program to check if the total number of divisors of a number is even or odd
- Check whether product of 'n' numbers is even or odd in Python

Advertisements