Ask Question
2 July, 12:06

What does the following fragment of code display? What do you think the programmer intended the code to do, and how would you fix it? int product = 1; int max = 20; for (int i = 0; i < = max; i++) product = product * i; System. out. println ("The product is " + product);

+4
Answers (1)
  1. 2 July, 14:20
    0
    0

    Explanation:

    Given the code segment:

    int product = 1; int max = 20; for (int i = 0; i < = max; i++) product = product * i; System. out. println ("The product is " + product);

    Since the counter i in the for loop started with 0, and therefore product * i will always be 0 no matter how many rounds of loop to go through.

    The code is likely to perform factorial calculation. To do so, we need to change the starting value for the counter i to 1.

    int product = 1; int max = 20; for (int i = 1; i < = max; i++) product = product * i; System. out. println ("The product is " + product);
Know the Answer?
Not Sure About the Answer?
Get an answer to your question ✅ “What does the following fragment of code display? What do you think the programmer intended the code to do, and how would you fix it? int ...” 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