Example 2: Check Even or Odd Numbers using recursion in Python
Another way to check if a number is odd or even is by using recursion. Recursion is a programming technique where a function calls itself until a specific condition is met. In this case, we'll use recursion to repeatedly subtract 2 from the number until it reaches 0 or 1. If the final value is 0, the original number is even; if it's 1, the number is odd.
Let’s see the code to check if a number is odd or even using recursion:
Python
def is_even_recursive(number):
if number == 0:
return True
elif number == 1:
return False
else:
return is_even_recursive(number - 2)
# Test the function
num = 7
if is_even_recursive(num):
print(num, "is even")
else:
print(num, "is odd")

You can also try this code with Online Python Compiler
Run Code
In this code, we define a recursive function called is_even_recursive that takes a number as input. The function has three base cases:
Output
7 is odd
- If the number is 0, we return True since 0 is even.
- If the number is 1, we return False since 1 is odd.
- If the number is neither 0 nor 1, we recursively call the function with the number reduced by 2.
The recursive calls continue until the number reaches either 0 or 1. If the final value is 0, the original number is even; if it's 1, the number is odd.
We test the function by providing a number (in this case, 7) and use an if-else statement to print whether the number is even or odd based on the return value of the is_even_recursive function.
Example 3: Check Even or Odd Numbers using & operator in Python
In addition to the modulo operator and recursion, we can also use the bitwise AND operator (&) to check if a number is odd or even. This method relies on the binary representation of numbers. In binary, even numbers always end with 0, while odd numbers always end with 1.
By performing a bitwise AND operation between a number and 1, we can determine if the number is odd or even. If the result is 0, the number is even; if the result is 1, the number is odd.
Let’s see the code to check if a number is odd or even using the bitwise AND operator:
Python
def is_even_bitwise(number):
if number & 1 == 0:
return True
else:
return False
# Test the function
num = 10
if is_even_bitwise(num):
print(num, "is even")
else:
print(num, "is odd")

You can also try this code with Online Python Compiler
Run Code
Output
10 is even
In this code, we define a function called is_even_bitwise that takes a number as input. Inside the function, we use an if-else statement to check if the result of the bitwise AND operation between the number and 1 is equal to 0. If it is, we return True, indicating that the number is even. Otherwise, we return False, meaning the number is odd.
We test the function by providing a number (in this case, 10) and use an if-else statement to print whether the number is even or odd based on the return value of the is_even_bitwise function.
Time and Space complexity for each of the above
Modulo operator
- Time complexity: O(1) The modulo operator performs a constant-time operation, regardless of the input size. It simply divides the number by 2 and checks the remainder, which takes a fixed amount of time.
- Space complexity: O(1) The space required is constant as well. We only need to store the input number and the result of the modulo operation, which takes a constant amount of memory.
Recursion
- Time complexity: O(n) The recursive approach has a time complexity that depends on the input number. In the worst case, when the input is an odd number, the function will recursively call itself n/2 times, where n is the input number. Each recursive call takes a constant amount of time, resulting in a linear time complexity.
- Space complexity: O(n) The space complexity for the recursive approach is also O(n) because the recursive calls are stored on the call stack. In the worst case, the call stack will have a depth of n/2, where n is the input number. Each recursive call takes a constant amount of space on the stack, resulting in a linear space complexity.
Bitwise AND operator
- Time complexity: O(1) The bitwise AND operation is a constant-time operation. It performs a single bitwise AND between the number and 1, regardless of the input size. Therefore, the time complexity is O(1).
- Space complexity: O(1) The space complexity is also constant. We only need to store the input number and the result of the bitwise AND operation, which takes a constant amount of memory.
In summary, both the modulo operator and bitwise AND operator approaches have a time and space complexity of O(1), making them very efficient for checking if a number is odd or even. On the other hand, the recursive approach has a time and space complexity of O(n), which can be less efficient for large input numbers.
Frequently Asked Questions
Can these methods be used to check if a negative number is odd or even?
Yes, all three methods can be used to check if a negative number is odd or even. The modulo operator, recursion, and bitwise AND operator work the same way for negative numbers as they do for positive numbers.
Is there a built-in function in Python to check if a number is odd or even?
No, there isn't a built-in function specifically for checking if a number is odd or even. However, you can easily implement this functionality using any of the methods discussed in this article.
Which method is the most efficient for checking if a number is odd or even?
The modulo operator and bitwise AND operator approaches are the most efficient, with a time and space complexity of O(1). The recursive approach has a time and space complexity of O(n), which can be less efficient for large input numbers.
Conclusion
In this article, we learned three different methods to check if a number is odd or even in Python: using the modulo operator, recursion, and the bitwise AND operator. We provided code examples for each approach and then analyzed their respective time and space complexity. The modulo operator and bitwise AND operator are the most efficient methods, while recursion can be less efficient for large input numbers.
Recommended Readings: