Would you tell me why this output is 2? Why not 0? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Would you tell me why this output is 2? Why not 0?

int[] a = {100, 89, 6,3}; for (int I = 1; I < a.length; I++){ a[I] = a[I - 1] %a[I]; } System.out.println(a[i]); }

11th Jul 2018, 2:08 PM
Afresia
Afresia - avatar
2 Answers
+ 1
The loop starts at 89 (index 1): Element 0 is unaffected Element 1 = 100 % 89 = 11 Element 2 = 11 % 6 = 5 Element 3 = 5 % 3 = 2 Now i equals 4, the loop stops, and the last value is printed, which is 2.
11th Jul 2018, 2:37 PM
Shadow
Shadow - avatar
+ 1
ooo, I got it, thank you Naitomea
11th Jul 2018, 2:46 PM
Afresia
Afresia - avatar