--- canonical_url: https://www.scaler.com/topics/even-odd-program-in-python title: Even Odd Program in Python - Scaler Topics description: This article by Scaler topics will discuss about even and odd numbers & even of program in python and examples using division strategy and bitwise operator. author: Sushant Gaurav category: Python amphtml: https://www.scaler.com/topics/even-odd-program-in-python/amp/ publish_date: 2022-04-08 --- # Even odd program in Python :::section{.abstract} ## 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 bitwise operator. ::: :::section{.scope} ## Scope The article contains topics such as: - an introduction to even and odd numbers. - even of program in python and examples using division strategy and bitwise operator. Each of the topics is explained clearly with diagrams and examples wherever necessary. ::: :::section{.main} ## Introduction Before learning about the even-odd program in python, we should first know 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 looking at the ending digit, we can easily check whether the number is even or not. **Example:** `74` is an even number. Since `74` ends with `4` and `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` and `71` leave the remainder `1` when divided by `2`. So, as we have seen, the number $2$ 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. ::: :::section{.tip} *Note:* The number `0` is considered an even number as `0` ends with one of the numbers (`0, 2, 4, 6, 8`). ::: :::section{.main} ## 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)** ```python Start If number mod = 0 Print "Number is Even" Else Print "Number is Odd" End ``` ::: :::section{.tip} *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. ::: :::section{.main} **Code:** ```python def even_odd(number): if (number % 2) == 0: print(f"{number} is even number.") else: print(f"{number} is odd number.") if __name__ == '__main__': number_1 = 71 even_odd(number_1) number_2 = 72 even_odd(number_2) ``` **Output:** ```python 71 is an odd number. 72 is an even number. ``` ### 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 **`~`, `<<` `>>`, `&`, `^`, `|`**. 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. Code: ```python def even_odd(number): if (number ^ 1 == number + 1): print(f"{number} is even number.") else: print(f"{number} is odd number.") if __name__ == '__main__': number_1 = 71 even_odd(number_1) number_2 = 72 even_odd(number_2) ``` **Output:** ```python 71 is an odd number. 72 is an even number. ``` ::: ### Check Even or Odd Numbers using recursion in Python We can also use recursion in even-odd programs in python. We will call the recursive function until it is greater than 1, it become 0 then called even and called odd. ```python def even_odd(n): if(n==0): return True elif(n==1): return False else: return evenOdd(n-2) if __name__ == '__main__': num=34 if(even_odd(num)): print(num," is even") else: print(num," is odd") ``` **Output:** ```python 34 is even ``` :::section{.summary} ## 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. :::