To calculate individual no. of even and odd numbers in any number | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
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

15th Apr 2022, 10:56 AM
Rid B
8 Answers
+ 4
Rid B I believe you are looking for something like this https://code.sololearn.com/cs6x15AmeIo4/?ref=app
15th Apr 2022, 8:02 PM
BroFar
BroFar - avatar
+ 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')}")
16th Apr 2022, 12:13 AM
Simba
Simba - avatar
+ 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!
15th Apr 2022, 3:09 PM
Lothar
Lothar - avatar
+ 2
Thanks a lot everyone who have helped me to get through this code :)
16th Apr 2022, 3:24 AM
Rid B
+ 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)}")
16th Apr 2022, 1:25 PM
Lothar
Lothar - avatar
+ 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.
15th Apr 2022, 12:15 PM
BroFar
BroFar - avatar
+ 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.... """
15th Apr 2022, 2:41 PM
Jayakrishna 🇮🇳
+ 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))
15th Apr 2022, 2:42 PM
Ervis Meta
Ervis Meta - avatar