+ 4

Understand this challenge

Hi I’ve been trying to understand this challenge. The output is 26 but I am figuring out how it got to that number. int x=1; for(; x<6; x++) x*=x; System.out.printf(“%d”,x) Any help would be appreciated.

24th Jan 2018, 10:22 AM
Tom
5 odpowiedzi
+ 25
//same x is increasing by mulplication & by addition of 1 loop 1)1×1 =1 [1 <6] then incremented by 1 loop2)2×2 =4 [2 <6] then incremented by 1 loop3)5×5 [5 <6] then incremented by 1 //condition becomes false [26 is not <6] & loop stops ... 26 printed //@Frost,Tom //hope it helps ☺👍
24th Jan 2018, 10:31 AM
Gaurav Agrawal
Gaurav Agrawal - avatar
+ 23
@Frost It has some problem. No semicolon.😊😉 System.out.printf(“%d”,x);
24th Jan 2018, 10:29 AM
Nithiwat
Nithiwat - avatar
+ 21
x=x*x will change everytime x changes/increases 1*1=> x=1 2*2=> x=4 5*5 => x=25 and then as x cannot be 6 it will go out of the loop after incrementing i.e x++ of 25 will give 26
24th Jan 2018, 10:25 AM
Frost
Frost - avatar
+ 11
First Loop:  x = 1 x*=x (same as x = x*x) = 1*1 = 1 x++ (increments by one) = 1+1 = 2 Second Loop: x = 2 x*=x = 2*2 = 4 x++ = 4+1 = 5 Third Loop: x = 5 x*=x = 5*5 = 25 x++ = 25 + 1 = 26 Exits Loop as x = 26  which is not < 6 (the condition is false)
24th Jan 2018, 10:51 AM
Pao
Pao - avatar
+ 3
Ahh. I understand it now. Thank you guys for the quick response. Amazing.
24th Jan 2018, 11:07 AM
Tom