A number is even if it is perfectly divisible by 2. When the number is divided by 2, we use the remainder operator %
to compute the remainder. If the remainder is not zero, the number is odd.
Source Code
# Python program to check if the input number is odd or even.
# A number is even if division by 2 gives a remainder of 0.
# If the remainder is 1, it is an odd number.
num = int[input["Enter a number: "]]
if [num % 2] == 0:
print["{0} is Even".format[num]]
else:
print["{0} is Odd".format[num]]
Output 1
Enter a number: 43 43 is Odd
Output 2
Enter a number: 18 18 is Even
In this program, we ask the user for the input and check if the number is odd or even. Please note that { }
is a replacement field for num
.
Python Program to Check if a Number is Odd or Even
Odd and Even numbers:
If you divide a number by 2 and it gives a remainder of 0 then it is known as even number, otherwise an odd number.
Even number examples: 2, 4, 6, 8, 10, etc.
Odd number examples:1, 3, 5, 7, 9 etc.
See this example:
Output:
Feedback
- Send your Feedback to [email protected]
Help Others, Please Share
Python Basic: Exercise-21 with Solution
Write a Python program to find whether a given number [accept from the user] is even or odd, print out an appropriate message to the user.
Pictorial Presentation of Even Numbers:
Pictorial Presentation of Odd Numbers:
Sample Solution:-
Python Code:
num = int[input["Enter a number: "]]
mod = num % 2
if mod > 0:
print["This is an odd number."]
else:
print["This is an even number."]
Sample Output:
Enter a number: 5 This is an odd number.
Even Numbers between 1 to 100:
Odd Numbers between 1 to 100:
Flowchart:
Visualize Python code execution:
The following tool visualize what the computer is doing step-by-step as it executes the said program:
Python Code Editor:
Have another way to solve this solution? Contribute your code [and comments] through Disqus.
Previous: Write a Python program to get a string which is n [non-negative integer] copies of a given string.
Next:
Write a Python program to count the number 4 in a given list.
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
We’ll discuss the above mentioned methods in detail in the next section.
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
Run
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.
Ternary Operator Syntax Python [ True : Action ] if [ Condition ] else [ False : Action ]
Python Code
Run
num = 17 print["Even"] if num%2 == 0 else print["Odd"]
Output
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.
Bitwise Operators In computer programming, a bitwise operation operates on a bit string, a bit array or a binary numeral at the level of its individual bits. It is a fast and simple action, basic to the higher-level arithmetic operations and directly supported by the processor.
Python Code
Run
def isEven[num]: return not num&1 if __name__ == "__main__": num = 13 if isEven[num]: print['Even'] else: print['Odd']
Output
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 150+ courses offered by PrepInsta in One Subscription
Get over 150+ 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
Checkout list of all the video courses in PrepInsta Prime Subscription
Checkout list of all the video courses in PrepInsta Prime Subscription
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