Python Program to Check Given Number is Odd or Even
This tutorial guides you on how to write python program to check given number is odd or even.
Python Program to Check Given Number is Odd or Even
To understand this example program, you should be aware of Python programming basics like Python if else statement and Python operators.
We are going to use Python if else and Python modulo operator in our example. The symbol “%” is called Modulo Operator in most of the programming languages including Python.
The modulo operator returns the remainder of dividing the left hand operand by the right hand operand. Therefore, you can simply say it returns the remainder of a division.
For example, the following program let you to enter any number and check whether the input number is odd or even. If the remainder is 1 then the given number is odd number. And when the number is perfectly divisible by 2 and the gives remainder 0, then it is even number.
Example – Check Number is Odd or Even
n = int(input("Enter any number: ")) if (n % 2) == 0: print("{0} is Even".format(n)) else: print("{0} is Odd".format(n))
Output 1:
Enter any number: 10 10 is Even
Output 2:
Enter any number: 13 13 is Odd
Note, if you don’t provide the proper input, then you will get invalid literal for int() with base 10 error as shown below. Therefore, make sure that you provide valid input.
Enter any number: asf --------------------------------------------------------------------------- ValueError Traceback (most recent call last) <ipython-input-134-c041f0854060> in <module> ----> 1 n = int(input("Enter any number: ")) 2 if (n % 2) == 0: 3 print("{0} is Even".format(n)) 4 else: 5 print("{0} is Odd".format(n)) ValueError: invalid literal for int() with base 10: 'asf'
Below is the screenshot of the same executed in Python Jupyter Notebook for your reference.
That’s it. Hope it helped π
You’ll also like:
- Convert floating point number to fixed point in Python
- What is %matplotlib inline and how to use ?
- Increase the cell width of the Jupyter Notebook in browser
- Add python3 kernel to jupyter IPython notebook ?
- Reset jupyter notebook theme to default theme
- How to change the default theme in Jupyter Notebook ?
- Change the Jupyter Notebook startup folder in Windows & Mac
- To run Jupyter Notebook on Windows from command line
- Run a Jupyter Notebook .ipynb file from terminal or cmd prompt
- Amazon Linux AMI : apt-get command not found
- Check if a file or folder exists without getting exceptions ?
- Python program to find the greatest of three numbers
- Putty Fatal Error No supported authentication methods available
- Find which users belongs to a specific group in linux
- Embed HTML within IPython Notebook
- Check if Python Object is a Number ?
- Remove non-numeric characters from string in Python
- Find difference between two given numbers in Python
- Convert negative to positive number in Python
- TypeError: a bytes-like object is required, not βstrβ β Python3
- Extract numbers from a string in python
- Java String substring() example program
References
Β