Before Solving a problem you have to know about odd or even numbers. So, A number divisible by Two is an even number and a number not divisible by Two is odd.
While solving this program you have to know about the basics of Python Programming, like Data Types, Variables, and Integers.
Program to find Odd or Even
Here is a basic program in Python to find if the given number is odd or even.
#Input
num = int(input("Enter a number: "))
#Check if the number is even
if num % 2 == 0:
print(f"The number {num} is Even.")
else:
print(f"The number {num} is Odd.")Sample output
Enter a number: 8
The number 8 is Even.- We directly take the user input and convert it to an integer.
- Then we use an if-else statement to check if the number is even or odd.
- If the remainder when dividing the number by 2 is 0, it’s even; otherwise, it’s odd.
- We print the result accordingly.
Using Function
Here is a program in Python to find if the given number is odd or even using a Function.
def fun(number):
if number % 2 == 0:
return "Even"
else:
return "Odd"
# Test the function
num = int(input("Enter a number: "))
result = fun(num)
print(f"The number {num} is {result}.")
Sample output
Enter a number: 3
The number 3 is Odd.- The ‘fun’ function takes a number as input and returns “Even” if the number is even and “Odd” if it’s odd.
- We use the modulo operator (%) to check if the remainder when dividing the number by 2 is 0.
- Depending on the result of the modulo operation, the function returns “Even” or “Odd”.
- Finally, we take user input, pass it to the function, and print the result.
