python program to calculate the count of odd and even digits in an integer.
Answers
Answered by
2
Answer:
Here comes the Python program for the question.
n=int(input("Enter a number: "))
x,y=0,0
while not n==0:
d=n%10
if d%2==1:
x+=1
else:
y+=1
n//=10
print("Number of Odd Digits: ",x)
print("Number of Even Digits: ",y)
Logic: Repeatedly divide the number by 10 and find the last digit of the number. Check if the last digit is odd or even. Count the number of odd and even digits and display it.
Another approach.
n=int(input("Enter a number: "))
l=[int(letter) for letter in str(n)]
x=sum([i%2 for i in l if i%2==1])
y=sum([i%2+1 for i in l if i%2==0])
print("Number of Odd Digits: ",x)
print("Number of Even Digits: ",y)
See the attachment for output ☑.
Attachments:
Similar questions