Ask Question
28 December, 08:44

Suppose sum and num are int variables, and the input is

18 25 61 6 - 1

What is the output of the following code?

Scanner cin = new Scanner (System. in); sum = 0; num = cin. nextInt (); while (num! = - 1) {sum = sum + num; num = cin. nextInt (); }System. out. println (sum);

+1
Answers (1)
  1. 28 December, 08:47
    0
    110

    Explanation:

    The Scanner is a Java class that is used to take an input from user.

    The nextInt () method will get an integer from user.

    The while loop in the question is a sentinel loop where the loop will keep running until user input the sentinel value which is - 1 to stop the loop. This is important to note that, before the start of next iteration, the program will prompt the input for an integer from user.

    While the loop is ongoing, the variable sum will be repeatedly added with input integer from user. If the input is 18, 25, 61, 6, the sum will be 18 + 25 + 61 + 6 = 110
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “Suppose sum and num are int variables, and the input is 18 25 61 6 - 1 What is the output of the following code? Scanner cin = new Scanner ...” in 📗 Computers & Technology if the answers seem to be not correct or there’s no answer. Try a smart search to find answers to similar questions.
Search for Other Answers