Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a loop that reads positive integers from standard input and that terminate

ID: 3652064 • Letter: W

Question

Write a loop that reads positive integers from standard input and that terminates when it reads an integer that is not positive. After the loop terminates, it prints out, separated by a space and on a single line, the sum of all the even integers read and the sum of all the odd integers read. Declare any variables that are needed.

ASSUME the availability of a variable, stdin , that references a Scanner object associated with standard input.

Explanation / Answer

int sumP=0; int sumO=0; int n; Scanner stdin = new Scanner (System.in); System.out.println("enter an odd or even number"); while (n = stdin.nextInt() >= 0){ if(stdin.nextInt()%2 == 0) sumP+= stdin.nextInt(); else sumO += stdin.nextInt(); } System.out.println(sumP + " " + sumO);

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote