What is the mistake in this Code.Why it is giving wrong output. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

What is the mistake in this Code.Why it is giving wrong output.

Sample Input 22 13 44 4 Sample Output 70 It needs to print sum of even int. https://code.sololearn.com/c4Jdl8is18kN/?ref=app

3rd Jul 2020, 2:47 AM
Akash
Akash - avatar
9 Answers
+ 3
You don't add the first value to sum. Just throw it away. Don't save it to a variable. It is just the number of inputs that will follow. That's why I said to re-read the code coach and see what the inputs are.
3rd Jul 2020, 6:13 AM
ChaoticDawg
ChaoticDawg - avatar
+ 4
Move this line outside of and just after while loop. System.out.print(sum);
3rd Jul 2020, 3:20 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
You're adding the first input as if it is part of the list. Re-read the challenge and pay attention to what the inputs are. Handle the first input correctly and you should have it solved.
3rd Jul 2020, 3:34 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
You just need to get the first input before the while loop and dispose of it as your while loop already takes care of the rest. Add sc.nextInt(); Before the loop. It doesn't need to be stored into an int, and it should pass all test cases.
3rd Jul 2020, 3:57 AM
ChaoticDawg
ChaoticDawg - avatar
+ 3
I am so Stupid.I should have readed what inputs are properly.I was frustated for So long for nonsense. Thank you for your help.You are very helpful.I had just now followed you.
3rd Jul 2020, 7:33 AM
Akash
Akash - avatar
+ 2
It still fails to satisfy case 4 and case 5 of That' odd in Code Coach.
3rd Jul 2020, 3:29 AM
Akash
Akash - avatar
+ 2
Please tell briefly what i need to change in the code as it is already only adding even integers.And How can i Change it.
3rd Jul 2020, 3:45 AM
Akash
Akash - avatar
+ 2
If I gets the first input before the while loop and classifies it as even or odd out of while loop.And Then adds it to the sum.It still shows that case 4 and case 5 are wrong Please Help! https://code.sololearn.com/c4Jdl8is18kN/?ref=app
3rd Jul 2020, 6:06 AM
Akash
Akash - avatar
+ 1
You didn't declare Variable i and so the compiler doesn't know what it is. It's that simple Solution:- Just initialise variable i like this. int i= 9 /* the value can be changed to whatever integer you choose */
4th Jul 2020, 8:36 AM
Theedon
Theedon - avatar