Write a Python Program to check whether a given number is even or odd.
What is an Even Number?
An even number is an integer that is divisible by 2 without leaving a remainder, such as 2, 4, 6, 8, 10, and so on.
What is an Odd Number?
An odd number is an integer that cannot be divided evenly by 2, leaving a remainder of 1. Examples include 1, 3, 5, 7, 9, and so on.
There are several ways to check whether a given number is even or odd in Python. Let’s take a detailed look at all the approaches to check whether a given number is even or odd.
In this approach, we check whether a number is even or odd using if-else statement and modulus operator.
Program/Source Code
Here is source code of the Python Program to determine whether a given number is even or odd using modulus operator. The program output is also shown below.
n = int(input("Enter a number: "))
if n % 2 == 0:
print(n, "is an even number.")
else:
print(n, "is an odd number.")
1. Prompt the user to enter a number.
2. Read the input number and store it in a variable n.
3. The program then uses the modulo operator % to check if the number divided by 2 leaves a remainder of 0.
4. If the remainder is 0, it means the number is divisible by 2 and hence even.
5. Otherwise, if the remainder is not 0, it is an odd number.
Time Complexity: O(1)
The time complexity of the program being O(1) indicates that the execution time remains constant, regardless of the input size.
Space Complexity: O(1)
The space complexity of the program being O(1) signifies that the code utilizes a fixed amount of memory to store the input number and other variables, irrespective of the input number’s magnitude.
Test case 1: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 10.
Enter a number: 10
10 is an even number.
Test case 2: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 5.
Enter a number: 5
5 is an odd number.
In this approach, we check whether a number is even or odd using bitwise operator.</p> <div class=”sf-codeh”>Program/Source Code
Here is source code of the Python Program to determine whether a given number is even or odd using bitwise operator. The program output is also shown below.
n = int(input("Enter a number: "))
if n & 1:
print(n, "is an odd number.")
else:
print(n, "is an even number.")
1. In this program, the user is prompted to enter a number.
2. The program then uses the bitwise AND operator (&) with 1 to check the least significant bit of the binary representation of the number.
3. If the result is non-zero, it means the number is odd.
4. Otherwise, if the result is zero, it is an even number.
Time Complexity: O(1)
The time complexity of the code is O(1) since it has a constant number of operations.
Space Complexity: O(1)
The space complexity is also O(1) as it uses a fixed amount of memory regardless of the input size.
Test case 1: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 6.
Enter a number: 6
6 is an even number.
Test case 2: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 9.
Enter a number: 9
9 is an odd number.
In this approach, we check whether a number is even or odd using recursion.
Here is source code of the Python Program to determine whether a given number is even or odd recursively. The program output is also shown below.
def check(n):
if (n < 2):
return (n % 2 == 0)
return (check(n - 2))
n=int(input("Enter number:"))
if(check(n)==True):
print("Number is even!")
else:
print("Number is odd!")
1. User must enter a number and store it in a variable.
2. The number is passed as an argument to a recursive function.
3. The base condition is that the number has to be lesser than 2.
4. Otherwise the function is called recursively with the number minus 2.
5. The result is returned and an if statement is used to check if the number is odd or even.
6. The final result is printed.
Time Complexity: O(n/2)
The time complexity of the code is O(n/2) since the function recursively calls itself n/2 times in the worst case.
Space Complexity: O(n)
The space complexity is O(n) due to the recursive function calls that create a stack frame for each call.
Test case 1: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 124.
Enter number:124
Number is even!
Test case 2: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 567.&lt;/p> &lt;div class=”hk1_style-wrap5″></p> <div class=”yoast-text-mark”>lass=”hk1_style-wrap4″>
Enter number:567
Number is odd!
In this approach, we check whether a number is even or odd using lambda function
Here is source code of the Python Program to determine whether a given number is even or odd using lambda function. The program output is also shown below.
check_number = lambda num: "even" if num % 2 == 0 else "odd"
number = int(input("Enter a number: "))
result = check_number(number)
print(number, "is an", result, "number.")
1. Define a lambda function named check_number that takes a parameter num.
2. The lambda function checks if num is divisible by 2 by using the modulo operator %.
3. If the remainder is 0, it returns the string “even”; otherwise, it returns the string “odd”.
4. Prompt the user to enter a number and store it in the variable number using int(input(“Enter a number: “)).
5. Call the lambda function check_number with number as the argument, and store the result in the variable result.
6. Print the number along with the result using the print function, indicating whether it is an odd or even number.
7. Example output: “12 is an even number.” or “17 is an odd number.”
Time Complexity: O(1)
The time complexity of the code is O(1) since the lambda function executes in constant time.
Space Complexity: O(1)
The space complexity is also O(1) as it uses a fixed amount of memory regardless of the input size.
Test case 1: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 100.
Enter a number: 100
100 is an even number.
Test case 2: Here is the runtime output of the program to check whether a number is even or odd when the user enters number = 55.
Enter a number: 55
55 is an odd number.