0
To calculate individual no. of even and odd numbers in any number
Can we calculate individual number of even and odd nubers in any input int n I am working on it since so long but cant get definite output, please help me https://code.sololearn.com/cb4P0bMJgEAw/?ref=app For example : input 5444 No. Of even terms : 3 No. Of odd terms : 1 As 444 are 3 even terms and 5 is odd term in input
8 Respuestas
+ 4
Rid B I believe you are looking for something like this
https://code.sololearn.com/cs6x15AmeIo4/?ref=app
+ 4
You can also use list comprehension .
oddeven = ["odd" if int(i)%2 else "even" for i in input()]
print(f"No.of even terms: {oddeven.count('even')}")
print(f"No.of odd terms: {oddeven.count('odd')}")
+ 2
Rid B ,
not quite clear to me what you wanted to achieve. can you please do an input sample and the expected output?
thanks!
+ 2
Thanks a lot everyone who have helped me to get through this code :)
+ 2
looks a bit like the solution from Simba, but first line is different:
res = [dig % 2 for dig in map(int, input())]
print(f"evens: {res.count(0)}, odds: {res.count(1)}")
+ 1
Rid B please provide us with your code so we can help you.
Yes the task is plausable but without seeing your code we have no idea where you went in error or how to fix.
+ 1
x=int(input("enter any number\n"))
a=0
b=0
y=0
print(x)
while x>0:
y = x % 10
if y % 2==0:
a+=1
else:
b+=1
x=x//10
print("evens:",a)
print("odds:",b)
"""
sample input : 12345
output :
evens : 2
odds : 3
#are you trying like this? Rid B
#can be more shorter....
"""
+ 1
# sorry for not understanding:
n = input('~> ')
if n.isdigit():
a,b = 0,0
for i in n:
if int(i)%2:
b+=1
else:
a+=1
print('Evens : %d\nOdds : %d\n'%(a,b))