Computer Science, asked by loganihrishi, 3 months ago

python program to calculate the count of odd and even digits. ​

Answers

Answered by anindyaadhikari13
2

Answer:

This is the required program for the question in Python.

First approach.

n=int(input("Enter a number: "))

a=b=0

while n!=0:

d=n%10

if d%2==0:

b+=1

else:

a+=1

n//=10

print("Odd digits:",a)

print("Even digits:",b)

Logic: Repeatedly divide the number by 10 and get the remainder. If the remainder is even, increment the value of even variable by 1 or else increment the value of odd variable by 1. Display the result.

Another approach using lists.

n=int(input("Enter a number: "))

x=[int(letter) for letter in str(n)]

odd=sum([1 for i in x if i%2==1])

even=len(x)-odd

print("Odd Digits:",odd)

print("Even Digits:",even)

Logic: Store the entered number in list. Increment the value of odd variable as many times as the numbers in the list is odd. Number of Even elements is equal to the length of the list - number of odd digits. Display the result.

See the attachment for output .

Attachments:
Similar questions