Python program to check even or odd number
Description: Write a Python program to check whether a number is even or odd.
Rules and hints:
- A number is even if it is divisible by 2.
- Use the modulus operator
%, which gives the remainder.
Python program to check even or odd
for number in (4, 7):
if number % 2 == 0:
print(number, "is even")
else:
print(number, "is odd")
Output:
Tutorials Class - Output Window
4 is even
7 is odd
How it works
4 % 2is 0, so 4 is even.7 % 2is 1, so theelsepart runs and 7 is odd.- Negative numbers also work, because
-3 % 2is 1 in Python.
Learn the basics in Python Conditional Statements.
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |