Sum of even numbers and product of odd numbers | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Sum of even numbers and product of odd numbers

If i took 5 numbers of list and taken numbers 10 20 30 40 50 then find sum of even numbers and product of odd numbers. Down 👇 this is my code but when i run it output came sum of even number 150 and product of odd number 1.Pls any one correct me. https://code.sololearn.com/cZpleqU7A57d/?ref=app

23rd Jun 2021, 10:00 AM
Sachin Pradhan
Sachin Pradhan - avatar
7 Answers
+ 2
co is variable that counts number of odd numbers in your list So co+=1 is increasing when number is odd (i.e else part indicates odd numbers) Then in line 18 I checked if co is 0 or not If co (number of odd numbers) is 0 then made pro (product of odd number) as 0
24th Jun 2021, 8:44 AM
Mayank Matwa
+ 1
In your input (i.e 10 20 30 40 50) there is no odd number so product of odd number display output 1 as you declared pro=1 in your code. So the solution can be you can add another variable to check if there are odd numbers in your list Or not. If there is no odd number then you can set your pro=0 so output of product of odd numbers comes out to be 0 instead of 1. Check this out https://code.sololearn.com/c05S1duf87q7/?ref=app
23rd Jun 2021, 10:37 AM
Mayank Matwa
+ 1
Thanks mayank matwa, this result i expected. 👍
24th Jun 2021, 5:06 AM
Sachin Pradhan
Sachin Pradhan - avatar
+ 1
But can you explain me that, in else part why you right co+=1?
24th Jun 2021, 5:11 AM
Sachin Pradhan
Sachin Pradhan - avatar
+ 1
Thanks you so much brother, good explain 👌now i understood perfectly.
24th Jun 2021, 3:34 PM
Sachin Pradhan
Sachin Pradhan - avatar
0
it works fine. enter it this way: 4 22 5 14 3
23rd Jun 2021, 10:21 AM
Slick
Slick - avatar
0
Sachin Pradhan what's the expected result if no odd numbers? (and what's the expected result if no even numbers) as stated by Mayank Matwa, you must take trace if no even and no odd values, then if either so, output specific value expected for the one wich are not presents ^^
23rd Jun 2021, 6:52 PM
visph
visph - avatar