Even Odd Program in Python
Learn via video course

Overview
Even numbers are the numbers that can be completely divided by 2 without leaving any remainder. On the other hand, odd numbers are those numbers that can not be completely divided by 2 (leaves a remainder when divided by 2). We can code the even-odd program in python using divisibility logic and a bitwise operator.
Scope
The article contains topics such as:
- an introduction to even and odd numbers.
- even of programs in python and examples using division strategy and bitwise operator.
Each of the topics is explained clearly with diagrams and examples wherever necessary.
Introduction
Before learning about the even-odd program in python, we should first know about the even and odd numbers.
Even numbers are the numbers that can be completely divided by 2 without leaving any remainder. The even numbers always end in 0, 2, 4, 6, or 8. If a number is very large, by just looking at the ending digit, we can easily check that the number is even or not.
Example:
74 is an even number. Since 74 ends with 4 as well as 74 are completely divisible by 2.
On the other hand, odd numbers are the numbers that can not be completely divided by 2 (leaves a remainder when divided by 2). The odd numbers always end in 1, 3, 5, 7, or 9. Just like even numbers, if a number is very large, we can easily check if the number is odd or not by looking at the ending digit.
Example:
71 is an odd number. Since 71 ends with 1 as well as 71 leave the remainder 1 when divided by 2.
So, as we have seen the number plays an important role in finding the even and odd numbers. So, let us code the even and odd program in python by using the divisibility logic.
Note: The number 0 is considered an even number. As 0 ends with one of the numbers (0, 2, 4, 6, 8).
Examples
Let us code the even and odd program in python.
Even Odd Program in Python Using Division
We will divide the given number by 2. If the number gets completely divided by 2 then the number is an even number. Else, the number is not an even number (i.e. odd number).
The pseudoscope for the even off program in python can be:
Function Name: even_odd(number)
Note: We can use the modulo operator in python % to find the remainder. The modulo operator in python returns the remainder of a division between two numbers.
Code:
Output:
Even Odd Program in Python Using Bitwise Operator
We can also use Bitwise Operators in even-odd programs in python. Bitwise Operators are the operators that help us to perform bit (bitwise) operations. Bit operators are ~, << >>, &, ^, |.
Note:
- ~ is the bitwise NOT operator that inverts all the bits of a number.
- >> is the right shift operator that shifts the number of places to the right.
- << is the left shift operator that shifts the number of places to the left.
- | is the bitwise OR operator that results in 1 if any of the two bits is 1.
- & is the bitwise AND operator that results in 1 if both of the two bits is 1.
Using the bitwise operator, we will check if the last bit of the number is set or not. If the last bit is set then the number must be odd. Else, the number will be an even number.
Refer to the image below for a better understanding and visualization of the XOR concept.
Code:
Output:
Conclusion
- Even numbers are the numbers that can be completely divided by 2 without leaving any remainder.
- On the other hand, odd numbers are the numbers that can not be completely divided by 2 (leaves a remainder when divided by 2).
- We can code the even-odd program in python using divisibility logic and bitwise operator.