What does the following fragment of code display? What do you think the programmer intended the code to do, and how would you fi x it? int product = 1; int max = 20; for (int i = 0; i <= max; i++) product = product * i; System.out.println("The product is " + product);
1 answer:
Answer:
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 <em>product * i </em>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);
You might be interested in
The USE statement switches to a different database.
Answer:
~CaptnCoderYankee
Don't forget to award brainlyest if I got it right!
Answer:
sandshrew is a cute character in pokemon
D. Rasterize This answer makes the most since
Answer:
typing on a key board
this is the best Ans.