Write a Python function, odd, that takes in one number and returns True when the number is odd and False otherwise. You should use the % (mod) operator, not if.
Don't spam
Answers
Answered by
2
Answer:
This is the required Python program for the question.
def odd(x):
return x%2==1
print("Is Odd? ",odd(int(input("Enter a number: "))))
Explanation:
- A number is said to be odd if it leaves a remainder 1 when it is divisible by 2. To get the remainder, the modulus operator is used (symbol - %).
- So, if a number is taken as input, say n, then the remainder obtained when the number is divided by 2 will be = n % 2.
- Now, an odd number leaves a remainder 1 when divided by 2. So, check if n % 2 == 1 or not. In this program,the function returns true if the remainder obtained is 1.
- After creating the function, we will ask the user to enter the number and then we will call it and the function displays the result accordingly.
Attachments:
Similar questions