Python Program to Check Whether a Number is Even or Odd
Check Whether a Number is Even or Odd in Python
Given an integer input num, the objective is to write a code to Check Whether a Number is Even or Odd in Python. To do so we check if the number is divisible by 2 or not, it’s Even if it’s divisible otherwise Odd.
Example
Input : num = 3
Output : Odd
Check Whether a Number is Even or Odd in Python
Given an integer input the objective is to write a Python code to Check Whether a Number is Even or Odd. To do so the main idea is to divide the number by 2 and check if it’s divisible or not. It’s an Even number is it’s perfectly divisible by 2 or an Odd number otherwise.
Here are the Methods to solve the above mentioned problem,
- Method 1 : Using Brute Force
- Method 2 : Using Ternary Operator
- Method 3 : Using Bitwise Operators
Method 1 : Using Brute Force
This method simply checks if the given input integer is divisible by 2 or not. If it’s divisible then print Even or Odd otherwise.
Python Code
num = int(input("Enter a Number:")) if num % 2 == 0: print("Given number is Even") else: print("Given number is Odd")
Output
Enter a Number: 5 Given number is Odd
Working
The working of the above mentioned code is as follows,
- Start
- Insert a number.
- If the given number is divisible by 2, then print,” Given number is even”.
- If the given number is not divisible by 2, then print ,”Given number is odd”.
- Stop
Explanation
Given an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not. If true, it’s even or it’s odd otherwise.
The algorithm for the above code is as given below,
- Import the required modules using import keyword.
- Initialize the required variables.
- Check if the number is divisible by 2, if true print even or odd otherwise using print() function.
The output for the above code is wither even or odd based on whether or not it’s divisible by 2.
Method 2 : Using Ternary Operator
This Method uses the ternary operator to check if the integer input is divisible by 2, If true print Even or Odd otherwise.
Python Code
num = 17 print("Even") if num%2 == 0 else print("Odd")
Output
Odd
Working
The working of the above code is as follows,
- Input an integer input num.
- Check whether the number is divisible by 2 using the ternary operator
- Ternary Operation, print(“Even”) if (num%2 == 0) else (print(“Odd”))
Explanation
Given an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not using a Ternary Operator in Python. If true, it’s even or it’s odd otherwise.
The algorithm for the above code is as given below,
- import the required modules using import keyword.
- Initialize the required variables.
- Check if the number is divisible by 2 using a Ternary Operator, if true print even or odd otherwise using print() function.
The output for the above code is wither even or odd based on whether or not it’s divisible by 2.
Method 3 : Using Bitwise Operator
This Method uses bitwise operators to check if a given number is Even or Odd.
Python Code
def isEven(num): return not num&1 if __name__ == "__main__": num = 13 if isEven(num): print('Even') else: print('Odd')
Output
Odd
Working
The working of the above code is as follows,
- If we have any number num doing bitwise ‘&‘ operation will give resultant as
- 1: If n is odd
- 0: if n is even
Explanation
Given an integer as input, the objective is check whether the number is even or odd in Python. To do so we check if it’s divisible by 2 or not using Bitwise Operator. If true, it’s even or it’s odd otherwise.
The algorithm for the above code is as given below,
- Import the required modules using import keyword.
- Define a function isEven() which returns a boolean variable to check if the number is even or odd.
- Initialize the required variables.
- Check if the function after calling returns True or False, if true print even or odd otherwise using print() function.
The output for the above code is wither even or odd based on whether or not it’s divisible by 2.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
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
Getting Started
- ASCII Table
- Positive or Negative number: C | C++ | Java | Python
- Even or Odd number: C | C++ | Java | Python
- Sum of First N Natural numbers: C | C++ | Java | Python
- Sum of N natural numbers: C | C++ | Java | Python
- Sum of numbers in a given range: C | C++ | Java | Python
- Greatest of two numbers: C | C++ | Java | Python
- Greatest of the Three numbers: C | C++ | Java | Python
- Leap year or not: C | C++ | Java | Python
- Prime number: C | C++ | Java | Python
- Prime number within a given range: C | C++ | Java | Python
- Sum of digits of a number: C | C++ | Java | Python
- Reverse of a number : C | C++ | Java | Python
- Palindrome number: C | C++ | Java | Python
- Armstrong number : C | C++ | Java | Python
- Armstrong number in a given range : C | C++ | Java | Python
- Fibonacci Series upto nth term : C | C++ | Java | Python
- Find the Nth Term of the Fibonacci Series : C | C++ | Java | Python
- Factorial of a number : C | C++ | Java | Python
- Power of a number : C | C++ | Java | Python
- Factor of a number : C | C++ | Java | Python
- Finding Prime Factors of a number : C | C++ | Java | Python
- Strong number : C | C++ | Java | Python
- Perfect number : C | C++ | Java | Python
- Automorphic number : C | C++ | Java | Python
- Harshad number : C | C++ | Java | Python
- Abundant number : C| C++ | Java | Python
- Friendly pair : C | C++ | Java | Python
number = int(input(“Enter the number: “))
if number % 2 == 0:
print(f”{number} is even”)
elif number % 2 != 0:
print(f”{number} is odd”)
n = int (input(“enter a number:”))
if n%2=0:
print(“given number is even”)
else:
print(“given number is odd”)
num=int(input(“enter a number”))
if num%2==0:
print(“The number is even”)
else:
print(“the number is odd”)
#user input
n = int(input(‘Enter your number:’))
#function declaration
def EvenOrOdd(n):
if(n%2==0):
print(‘Even number’)
else:
print(‘Odd number’)
#function calling
EvenOrOdd(2)
#output
even number
a = int(input(“Enter The Number To Find:\n”))
if a%2==0:
print(“The Number Is Even”)
else:
print(“The Number Is odd”)
n = int(input(“Enter the number for check : “))
if n%2== 0:
print(“Number is Even “)
else:
print(“Number is Odd “)
num=int(input(“enter the number:”))
if num%2 == 0:
print(str(num) + ” num is even”)
else:
print(str(num) + ” num is odd”)
numb = int(input(“Enter the Number: “))
if numb % 2 == 0:
print(f”Number {numb} is Even Number”)
else:
print(f”Number {numb} is Odd Number”)
I =int(input (‘enter the no: ‘)
if(&1):
Print(“odd”)
Else:
Print(“even”)