Suppose that the input is: 38 45 71 4 -1 What is the output of the following cod
ID: 3608286 • Letter: S
Question
Suppose that the input is: 38 45 71 4 -1 What is the output of the following code (Assume allvariables are properly declared): sum = console.nextInt(); num = console.nextInt(); while (num ! = -1) { sum = sum + num; num =console.nextInt(); } System.out.println("Sum = " + sum); Please explain how you arrive at thissolution Suppose that the input is: 38 45 71 4 -1 What is the output of the following code (Assume allvariables are properly declared): sum = console.nextInt(); num = console.nextInt(); while (num ! = -1) { sum = sum + num; num =console.nextInt(); } System.out.println("Sum = " + sum); Please explain how you arrive at thissolution { sum = sum + num; num =console.nextInt(); } System.out.println("Sum = " + sum); Please explain how you arrive at thissolutionExplanation / Answer
It is 158.
Reason:
intialy sum =38 num =45
now in loop it sum to numuntil num is -1. so it willexecute
Iteration1 num = 45 and sum= sum +num = 38 + 45 = 83 Num has changed to71
Iteration2 num = 71 and sum= sum +num = 83 + 71 = 154 Num has changed to4
Iteration3 num = 4 and sum= sum +num = 154 + 4 = 158 Num has changed to-1
Iteration1 num = 45 and sum= sum +num = 38 + 45 = 83 Num has changed to71
Iteration2 num = 71 and sum= sum +num = 83 + 71 = 154 Num has changed to4
Iteration3 num = 4 and sum= sum +num = 154 + 4 = 158 Num has changed to-1
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.