Write a loop that reads positive integers from standard input and that terminate
ID: 3652065 • 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, on a line by itself, the sum of all the even 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);
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.