Search

## Python Program: To Find if a given number is Odd or Even

In this article, learn how to write a Python program to determine whether a given number is odd or even. Understand the logic and implementation behind this program and enhance your coding skills. Discover how conditional statements and the modulus operator are used to check for divisibility and classify numbers. Improve your understanding of Python’s control flow and decision-making capabilities with this simple programming exercise.

Let’s check another Python Project – Counting The Number Of Digits.

#### Table of Contents

Prerequisites to move ahead:

Conditional Statements (If… else)

Loop (while loop, for loop)

Operator (==, %, !=)

### Using Conditional Statements

Example: Using Conditional Statements.

```				```
number = int(input("Enter a number: "))

if number % 2 == 0:
print(number, "is an Even number.")
else:
print(number, "is an Odd number.")
```
```

Explanation:

The program prompts the user to enter a number using the input() function. The int() function converts the user input, which is initially a string, into an integer data type.

If statement is used to check the condition (number % 2 == 0) whether a given number is exactly divisible by 2 or not. The operator modulus % returns the remainder when the number is divided by 2. If the remainder is 0, the number is divisible by 2, indicating an even number.

If the condition is true, the program executes the code block inside the if statement and prints entered number is even, along with the actual number.

If the condition is false, meaning that the remainder is not 0. The program executes the else statement and prints entered number as odd, along with the actual number.

Finally, now the program then terminates, and the output is displayed on the console based on whether the entered number is odd or even.

### Using a while loop

Example: Using a while loop.

```				```
while True:
number = int(input("Enter a number: "))

if number % 2 == 0:
print(number, "is an Even number.")
else:
print(number, "is an Odd number.")

ask_user = input("Do you want to check again? y/n --> ")

if ask_user != "y":
print("Thanks for checking. Have a great day ahead!")
break
```
```

Explanation:

Since the code of the block is inside the while loop, the program keeps executing forever until the user stops.

The explanation of the above program is the same as in example 1. However, the program asks the user if they want to continue checking the number; once done, if the user enters “y”, the program executes again, or it terminates the program.

### Using a range function

Example: Using a range function.

```				```
for number in range(0, 11):
if number % 2 == 0:
print(number, "is an Even number.")
else:
print(number, "is an Odd number.")
```
```

Explanation: The program checks the condition within a range of numbers, i.e., 1 to 11, and prints the output.

Note: The last number is always excluded from the range function, i.e., 11 in the above example.

### Using range function and while loop

Example: Using range function and while loop.

```				```
for number in range(0, 6):
if number % 2 == 0:
print(number, "is an Even number.")
else:
print(number, "is an Odd number.")

user_input = input("Do you want to try by yourself? y/n --> ")

if user_input.lower() != "y":
print("Have a great day ahead!")

else:
while True:
number = int(input("Enter a number: "))

if number % 2 == 0:
print(number, "is an Even number.")
else:
print(number, "is an Odd number.")

ask_user = input("Do you want to check again? y/n --> ")

if ask_user != "y":
print("Thanks for checking. Have a great day ahead!")
break
```
```

Explanation: The explanation of the above program is the same as the above example. However, the program asks the user if they want to continue checking the number; once done from the range of numbers, if the user enters “y”, then the program executes again, or it terminates the program.

### Conclusion

The article demonstrates a Python program to determine whether a given number is odd or even. The code efficiently utilizes conditional statements and the modulus operator to perform the check. By prompting the user to input a number, the program applies the modulus operator (%) to determine if the remainder of dividing the number by 2 is zero. If the remainder is zero, the program concludes that the number is even; otherwise, it is considered odd. The program then displays the appropriate message indicating whether or not the inputted number is odd or even.

This simple yet effective program showcases basic control flow and decision-making in Python. It provides a solid foundation for understanding and implementing similar logic in complex programs.

Recent Articles