+ 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
7 ответов
+ 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
+ 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
+ 1
Thanks mayank matwa, this result i expected. 👍
+ 1
But can you explain me that, in else part why you right co+=1?
+ 1
Thanks you so much brother, good explain 👌now i understood perfectly.
0
it works fine.
enter it this way:
4
22
5
14
3
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 ^^