Reading integers and find the sum | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Reading integers and find the sum

Program with several integers Write a Java program that reads in several integers until sentinel value of 0 is entered. The program must calculate and display the sum of all the even numbers. The program must calculate and display the sum of all odd numbers. The output should look something like this: The sum of the even numbers is: ___ The sum of the odd members is: ___ This is what I have so far.. Scanner in = new Scanner(System.in); int sum = 0; int input; System.out.println(“Enter values, enter 0 to end”); input = in.nextInt(); while(input > 0) { sum = input + sum; } ................. All I can think is how to do a sum for all the numbers. I’m not sure how to separate the odd and even.

31st Oct 2020, 1:18 AM
Jay Elle
2 Answers
+ 4
Create two variable of type int named odd and even. if(input%2==0){ //Even even+=input; }else{ /Odd odd+=input; }
31st Oct 2020, 1:40 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar
+ 3
Ipang thanks for reminding.
31st Oct 2020, 3:46 AM
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~
0_O-[MĂ€gĂĄr_SĂĄm_ÄkĂ _NĂŒllpÞïntĂȘr_ÈxĂ«cĂ©ptïön]~~ - avatar