Ask Question
23 January, 22:00

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. 23 January, 23:45
    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 and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers