+ 14

I NEED explaination

int n=2; int m=4; for (int i=2;i<m;i++){ n=n*n; } System.out.println(n); Output is 16. Please explain it.

11th Dec 2017, 9:31 AM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
11 Réponses
+ 10
i=2: n=2*2=4 i=3: n=4*4=16 loop terminates. prints n
11th Dec 2017, 9:36 AM
👑 Prometheus 🇸🇬
👑 Prometheus 🇸🇬 - avatar
+ 26
nothing else to write 👍😉
11th Dec 2017, 9:44 AM
tooselfish
tooselfish - avatar
+ 7
As value of i goes from i=2 to i=3, the for loop will run twice Firstly, n = 2*2 = 4 Secondly, n = 4*4 = 16 And then it prints the value of n -> 16
11th Dec 2017, 9:36 AM
#RahulVerma
#RahulVerma - avatar
+ 6
Thanks you all😉😉
11th Dec 2017, 1:14 PM
Muhd Khairul Amirin
Muhd Khairul Amirin - avatar
+ 5
Here, n=2 and m=4; The loop starts from 2 and continues until i<4; so, when i=2....n=n*n=n=2*2=4; again, when i=3....n=n*n=4*4=16 so output is 16
11th Dec 2017, 9:39 AM
Md. Nafis Ul Haque Shifat
Md. Nafis Ul Haque Shifat - avatar
+ 5
Here I value should be 2 so 2 times only execute for loop so u get the n value is 16..
11th Dec 2017, 10:49 AM
Pradheepviki😎😀
Pradheepviki😎😀 - avatar
+ 4
1)n = 2 × 2, i = 2 2)n =4 × 4, i = 3 n = 16
11th Dec 2017, 9:36 AM
Dextozz
Dextozz - avatar
+ 4
for(I=0;I<m;I++) u get the output is 65536 I=0 n=2 n=n*n n=2*2=4 next I should be increment by 1 now n=4 4*4=16 next I value is 2 now n=16 16*16=256 now I value is 3 now n=256 256*256=65536 next I value is 4 it should not be less than m value it is equal, so came outside for loop and print n value.
11th Dec 2017, 10:47 AM
Pradheepviki😎😀
Pradheepviki😎😀 - avatar
0
+6
11th Dec 2017, 6:43 PM
Ibrahim Abdulaahi Kherow
Ibrahim Abdulaahi Kherow - avatar
0
+5
11th Dec 2017, 6:43 PM
Ibrahim Abdulaahi Kherow
Ibrahim Abdulaahi Kherow - avatar
0
@pegasus made damn good response :)
12th Dec 2017, 10:26 PM
Дејан Борковић
Дејан Борковић - avatar