Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to Check if a Number is Odd or Even using Python?
In this article, we will show you how to check if a number is odd or even in python. Below are the methods to accomplish this task ?
- Using modulo (%) operator
- Using Recursion
- Using the Binary AND (&) operator
Using modulo (%) operator
Python's modulo (%) operator (also called the remainder operator) is useful to determine if a number is odd or even. We obtain the remainder of the division of a number by 2. If it is 0, it is even otherwise it is odd
Even Number ? A number that can be divided by 2, leaving no remainders(remainder=0).
Odd Number ? An odd number is one that cannot be divided by 2, so the remainder is 1.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Create a variable to store the input number.
Use the if conditional statement to check whether the input number modulus 2 is equal to 0 using the modulus (%)operator(returns the remainder).
Print even if the condition is true i.e, the remainder is 0.
Else Print odd if the condition is false i.e, the remainder is not 0.
Example
The following program returns the whether the input number is an even or an odd number using the modulo (%) operator ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># input number</span> inputNumber <span class="token operator">=</span> <span class="token number">10</span> <span class="token comment"># checking whether the number modulus 2 is equal to 0</span> <span class="token keyword">if</span> inputNumber<span class="token operator">%</span><span class="token number">2</span><span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">:</span> <span class="token comment"># printing even if the remainder is 0</span> <span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an even number"</span><span class="token punctuation">)</span> <span class="token keyword">else</span><span class="token punctuation">:</span> <span class="token comment"># else printing odd</span> <span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an odd number"</span><span class="token punctuation">)</span> </div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output ?
10 is an even number
Using Recursion
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Create a function checkEvenOdd to check whether the number passed to it as an argument is an even or odd number.
Use the if conditional statement to check whether the number is 0 and if it is 0 then the given number is even so return True.
Use another if conditional statement to check whether the number is 1 and if it is 1 then the given number is odd so return False.
Call the function again recursively by subtracting 2 from the given number.
Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
Print even if the function returns True.
Else print odd if the function returns False
Example
The following program returns the whether the input number is an even or an odd number using the recursive function ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function that accepts a number as an argument and</span>
<span class="token comment"># checks whether it is an odd or even number</span>
<span class="token keyword">def</span> <span class="token function">checkEvenOdd</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># checking whether the number is 0</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>num<span class="token operator">==</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># returning True, if the number is even</span>
<span class="token keyword">return</span> <span class="token boolean">True</span>
<span class="token comment"># checking whether the number is 1</span>
<span class="token keyword">elif</span><span class="token punctuation">(</span>num<span class="token operator">==</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># returning False, if the number is odd</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token comment"># Calling the function again recursively by subtracting 2 from the given number</span>
<span class="token keyword">return</span> checkEvenOdd<span class="token punctuation">(</span>num<span class="token operator">-</span><span class="token number">2</span><span class="token punctuation">)</span>
<span class="token comment"># input number</span>
inputNumber<span class="token operator">=</span> <span class="token number">7</span>
<span class="token comment"># passing inuput number as an argument to the checkEvenOdd() and calling it</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>checkEvenOdd<span class="token punctuation">(</span>inputNumber<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># printing even if the function returns true</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an even number"</span><span class="token punctuation">)</span>
<span class="token keyword">else</span><span class="token punctuation">:</span>
<span class="token comment"># else printing odd if the function returns false</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an odd number"</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output ?
7 is an odd number
Using Binary AND (&) operator
The idea is to check if the last bit of the number is set. If the last bit is set, the number is odd, otherwise it is even.
As you can see, bitwise ANDing(&) a number through a 1 gives a 1 if the number is odd because the last bit is already set. Otherwise, 0 is returned as output.
Algorithm (Steps)
Following are the Algorithm/steps to be followed to perform the desired task ?
Use the if conditional statement to check whether the binary and(&) operation between the number passed and 1 is equal to 0 using the & operator.
If the condition is true, then the number is even and returns True.
Else the given number is odd so return False.
Create a variable to store the input number.
Pass the input number as an argument to the checkEvenOdd() and call the function. Use the if conditional statement to check whether the function returns True or False.
Print even if the function returns True.
Else print odd if the function returns False.
Example
The following program returns the whether the input number is an even or an odd number using the binary AND (&) operator ?
<div class="execute"></div><div class="code-mirror language-python" contenteditable="plaintext-only" spellcheck="false" style="outline: none; overflow-wrap: break-word; overflow-y: auto; white-space: pre-wrap;"><span class="token comment"># creating a function that accepts a number as an argument and</span>
<span class="token comment"># checks whether it is an odd or even number</span>
<span class="token keyword">def</span> <span class="token function">checkEvenOdd</span><span class="token punctuation">(</span>num<span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># checking whether num&1 == 0</span>
<span class="token keyword">if</span> num <span class="token operator">&</span> <span class="token number">1</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">:</span>
<span class="token comment"># Then the number is even so return True</span>
<span class="token keyword">return</span> <span class="token boolean">True</span>
<span class="token comment"># Else the number is odd</span>
<span class="token comment"># Then the number is odd so return False</span>
<span class="token keyword">return</span> <span class="token boolean">False</span>
<span class="token comment"># input number</span>
inputNumber<span class="token operator">=</span> <span class="token number">12</span>
<span class="token comment"># passing input number as an argument to the checkEvenOdd() and calling it</span>
<span class="token keyword">if</span><span class="token punctuation">(</span>checkEvenOdd<span class="token punctuation">(</span>inputNumber<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">:</span>
<span class="token comment"># printing even if the function returns true</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an even number"</span><span class="token punctuation">)</span>
<span class="token comment"># else printing odd if the function returns false</span>
<span class="token keyword">print</span><span class="token punctuation">(</span>inputNumber<span class="token punctuation">,</span> <span class="token string">"is an odd number"</span><span class="token punctuation">)</span>
</div><div class="output-wrapper"><div class="console-close"></div><div class="code-output"></div></div>
Output
On executing, the above program will generate the following output ?
12 is an even number 12 is an odd number
Conclusion
In this article, we learned how to use three different methods to determine whether a given number is even or odd. We learned how to check the last bit of a given number using bitwise operators. We learned how to recursively call the function.
