Python program to check if a number is Odd or Even

Introduction
In Python, you can easily determine whether a number is odd or even using a simple conditional statement. In this tutorial, we will show you how to create a Python program that checks the parity of a given number.
Understanding how to check if a number is odd or even is not only a fundamental concept but also a building block for more complex programs. By the end of this tutorial, you’ll have a clear grasp of the necessary Python syntax and logic required to tackle this problem. So, let’s dive in and learn.
Concept of Even Number and Odd Number
Even Number
An even number is any integer that is exactly divisible by 2, meaning that when you divide it by 2, there is no remainder.
Examples of even numbers include 2, 4, 6, 8, 10, and so on.
Odd Number
An odd number is any integer that is not exactly divisible by 2, meaning that when you divide it by 2, there is a remainder.
Examples of odd numbers include 1, 3, 5, 7, 9, and so on.
Example :

Example 1:
Input: num = 12
Output: Even
Example 2:
Input: n = 9
Output: Odd
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Python program to check if a number is odd or even
# Input a number from the user num = int(input("Enter a number: ")) # Check if the number is even or odd if num % 2 == 0: print(str(num) + " is even.") else: print(str(num) + " is odd.")
Output :
Enter a number: 10 10 is even.
Explanation :
- The program takes a number as input from the user.
- It calculates the remainder of the number when divided by 2 using the modulus operator (%).
- If the remainder is 0, the number is even; otherwise, it is odd.
- The result (odd or even) is then printed as output.
- This approach efficiently determines the parity of any integer.
Time and space complexity:
Operation | Time Complexity | Space Complexity |
---|---|---|
Input Handling | O(1) | O(1) |
Modulo Operation | O(1) | O(1) |
Print Statement | O(1) | O(1) |
Overall | O(1) | O(1) |
Python program to check if a number is odd or even using Recursion
def is_even(num): if num == 0: return True elif num == 1: return False else: return is_even(num - 2) # Input a number from the user num = int(input("Enter a number: ")) # Check if the number is even or odd using recursion if is_even(num): print(str(num) + " is even.") else: print(str(num) + " is odd.")
Output :
Enter a number: 10 10 is even.
Explanation :
- The program prompts the user to input a number.
- It defines a recursive function that repeatedly subtracts 2 from the number.
- If the function reaches 0, the number is even; if it reaches 1, the number is odd.
- The result (odd or even) is displayed to the user.
- This method demonstrates how recursion can be used to check a number’s parity.
Time and space complexity:
Operation | Time Complexity | Space Complexity |
---|---|---|
Recursive Even Check | O(n) | O(n) |
Overall | O(n) | O(n) |
Python program to check if a number is odd or even using function
# Define a function to check if a number is odd or even def check_odd_even(num): if num % 2 == 0: return "even" else: return "odd" # Input a number from the user num = int(input("Enter a number: ")) # Call the function to check if the number is odd or even result = check_odd_even(num) # Display the result print(str(num) + " is " + result + ".")
Output :
Enter a number: 18 18 is even.
Explanation :
- The program defines a function check_odd_even to check if a number is odd or even.
- It uses the modulo operator (%) to determine evenness.
- The function returns a string — either “even” or “odd” — based on the result.
- The user enters a number, and the function is called with this input.
- The final result is displayed using string concatenation.
Time and space complexity:
Operation | Time Complexity | Space Complexity |
---|---|---|
Modulo Check | O(1) | O(1) |
Overall | O(1) | O(1) |
To wrap it up:
In this Python program, we have created a simple yet powerful tool to distinguish between even and odd numbers. Understanding the concept of even and odd numbers is fundamental in mathematics and has practical applications across various fields, including programming, data analysis, finance, and more.
FAQs
You can check using the modulo operator %. If num % 2 == 0, it means the number is divisible by 2 and is even; otherwise, it’s odd. This is the most common and straightforward method.
Yes, you can use the expression num & 1. If the result is 0, the number is even; if the result is 1, it’s odd. This is a faster, low-level approach often used in performance-critical code.
The same logic works for negative numbers. For example, -4 % 2 == 0 confirms it’s even, while -3 % 2 != 0 shows it’s odd. Python handles modulo operation correctly for both positive and negative values.
Yes, by recursively subtracting 2 until the number becomes 0 (even) or 1 (odd). Though this works, it’s not memory-efficient and may cause stack overflow for very large inputs.
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment