+ 8

How the output is 26???

public class Program { public static void main(String[] args) { int x=1; for(;x<6;x++) x*=x; System.out.printf("%d",x); } }

24th Feb 2018, 3:51 PM
Rishabh
Rishabh - avatar
3 Answers
+ 6
I'll try breaking down the process that the code is run - 1. An int x is initialized with a value of 1 2. A for loop is entered 3. The second condition returns true (x is less than 6), resulting in the statements to be run within it 4. x gets multiplied by itself 5. x increments 6. x now equals 2 (1 * 1 = 1, 1 + 1 = 2) 7. x is still less than 6 8. x gets multiplied by itself 9. x increments 10. x now equals 5 (2 * 2 = 4, 4 + 1 = 5) 11. x is still less than 6 12. x gets multiplied by itself 13. x increments 14. x now equals 26 (5 * 5 = 25, 25 + 1 = 6) 15. x is now not less than 6 16. The value of x is printed Hope this helped!
24th Feb 2018, 4:16 PM
Faisal
Faisal - avatar
+ 15
1 * 1 = 1 1+ 1 = 2 2 * 2 = 4 4 + 1 = 5 5 * 5 = 25 25 + 1 = 26
24th Feb 2018, 4:11 PM
Vukan
Vukan - avatar
+ 2
put your code in playground.. so we could help
24th Feb 2018, 4:12 PM
Farshaad Heydari
Farshaad Heydari - avatar