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.
2 Respuestas
+ 4
Create two variable of type int named odd and even.
if(input%2==0){
//Even
even+=input;
}else{
/Odd
odd+=input;
}
+ 3
Ipang thanks for reminding.