Ask Question
30 October, 07:35

What is the output of the following code fragment? int i = 1; int sum = 0; while (i < = 15) { sum = sum + i; i++; } System. out. println ("The value of sum is " + sum);

+2
Answers (1)
  1. 30 October, 08:22
    0
    The value of sum is 120

    Explanation:

    Consider the same code snippet numbered below:

    int i = 1; int sum = 0; while (i < = 15) { sum = sum + i; i++; } System. out. println ("The value of sum is " + sum); }

    Line 1 Initializes an int variable i to 1

    Line 2 creates another int variable sum, sets it to 0

    line 3 has a while statement that executes until i becomes equal to 15

    In line 4 the current value of sum is continuously added to i as long as the condition in line 3 stays true.

    Line 5 ensures there is no infinite loop by increasing i by 1

    At line 7 The value of the variable sum will be 120. This is the sum of numbers from 1 to 15. and it is printed to the console.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question ✅ “What is the output of the following code fragment? int i = 1; int sum = 0; while (i < = 15) { sum = sum + i; i++; } System. out. println ...” 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