Ask Question
17 August, 06:54

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);

+5
Answers (1)
  1. 17 August, 07:50
    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?
Get 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 there is no answer or all answers are wrong, use a search bar and try to find the answer among similar questions.
Search for Other Answers