python

Check if a number is odd or even in Python

In this tutorial, we are going to see how to check if a number is odd or even in Python. If a number is divisible by 2 without remainder, then it is even. You can calculate the remainder with the modulo % operator like this n%2 == 0. If a number divided by 2 leaves a remainder of 1, the number is odd. You can check this by using n%2 == 1. The code below asks the user to enter any number to check if the current value is even or odd.
 

 

How to check if a number is odd or even in Python
# Asks the user to enter a number
n = int(input("Enter a number: "))

if (n % 2) == 0:
   print("{0} is even".format(n))
else:
   print("{0} is odd".format(n))

Output:

Enter a number: 2
2 is even

Enter a number: 3
3 is odd
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *