input a three digit number and check whether the middle digit is odd or even?(plz explain In details)
Answers
Answered by
5
Language:
Python
Logic:
Let the number be abc (a three digit number).
First thing you need to do is retrieve the middle number and for that we'll be using
a)% (modulus operator) which return the remainder
b) // opertaor to get the quotient.
first abc%100 = bc (eg. 125 on dividing with 100 returns remainder 25)
now bc//10 = b (eg. 25 on being divided by 10 returns quotient 2)
Now to check if the number is even or odd use % with 2 eg. 2%2 is the output value is o ( i.e reminder is 0) the number was even and if not it was odd (Eg 5%2=1 hence odd).
Program:
a=int(input("Enter a 3-digit number: "))
if (a%100//10)%2==0:
print(a%100//10,"is even.")
else:
print(a%100//10,"is odd.")
Output:
Enter a 3-digit number: 125
2 is even.
Attachments:
Attachments:
Similar questions