Python Program to Check if a Number is Odd or Even
Odd and even numbers are types of numbers that can be classified based on whether they are divisible by 2 or not1234.
An even number is a number that can be divided by 2 without leaving any remainder1. For example, 2, 4, 6, 8, 10… are even numbers. Even numbers always end with 0, 2, 4, 6 or 812.
An odd number is a number that cannot be divided by 2 without leaving a remainder of 11. For example, 1, 3, 5, 7, 9… are odd numbers. Odd numbers always end with 1, 3, 5, 7 or 9
Python Code :
The below Python program checks if a number is odd or even:
# Get input number from user
num = int(input("Enter a number: "))
# Check if the number is odd or even
if num % 2 == 0:
print("The number", num, "is even")
else:
print("The number", num, "is odd")
In this program, the user is prompted to enter a number. The program then checks whether the number is odd or even using the modulus operator %. If the number is divisible by 2 (i.e., the remainder of num % 2 is 0), it is even. Otherwise, it is odd.
Finally, the program prints out the result using the print function.
Note that this program assumes that the user enters a valid integer.
If the user enters an invalid input, such as a string or a non-numeric value, the program will raise a ValueError or produce an incorrect result. Also, note that the program works only for integers, not for floating-point numbers.
For example :
Enter a number: 30
The number 30 is even
Enter a number: 21
The number 21 is odd