Ask Question
21 November, 10:34

What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = i + i; i = i + 1; j = j - 1; j = j - j; count++; } System. out. println ("i = " + i + ", j = " + j);

+2
Answers (1)
  1. 21 November, 10:45
    0
    i = 351, j = 0

    Step-by-step explanation:

    The while loop runs 5 times as indicated by the variable, count.

    The variable, i, has an initial value of 10. The first line of the loop code doubles the value of i while the second increments it by 1. This is done 5 times. We have

    Iteration 1: i = 10 + 10 + 1 = 21

    Iteration 1: i = 21 + 21 + 1 = 43

    Iteration 1: i = 43 + 43 + 1 = 87

    Iteration 1: i = 87 + 87 + 1 = 175

    Iteration 1: i = 175 + 175 + 1 = 351

    The third line of the loop code decrements j by 1. However, the fourth line sets j = 0 by subtracting it from itself. Hence, j is always 0 at the end of the loop, no matter its initial value or the number of iterations.

    Thus, at the end of the code snippet, i = 351 and j = 0.
Know the Answer?
Not Sure About the Answer?
Find an answer to your question 👍 “What are the values of i and j after the following code snippet is run? int i = 10; int j = 20; int count = 0; while (count < 5) { i = i + ...” in 📗 Mathematics 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