Write a simple program to accept a value from the user & display whether it is even or odd.
This question is from python program
Sub: Computer
Share
Write a simple program to accept a value from the user & display whether it is even or odd.
This question is from python program
Sub: Computer
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Answer:
The given program is written in Python 3.
n=int(input("Enter a positive number: "))
if n%2==0:
print(n,"is even.")
else:
print(n,"is odd.")
Logic:
Logic is very simple. A number is said to be even if it is divisible by 2 whereas a number is said to be odd if it is not divisible by 2.
If the number is divisible by 2, then remainder must be 0.
→ Number divided by 2 leaves remainder = 0
To calculate remainder obtained, the modulo operator is used (%).
So, if n is a number, then remainder on dividing n by 2 will be = n%2
If n%2 is 0, number is even else odd. Using this logic, problem is solved.
Refer to the attachment.
•••♪