What is the output of this code? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

What is the output of this code?

When I challenged in Java there was a question but I couldn't understand the question. Here is the question: What is the output of this code? int x=1 for(; x<6; x++) x*=x; System.out.printf("%d", x) The answer is 26. How is it 26 and what is" %d"here?

14th Apr 2020, 7:16 AM
Umidbek
Umidbek - avatar
2 Answers
0
the iterator x has been initialized before the loop, that is why the first part is empty. in a for loop after each execution of the statement, the iterator is incremented, so we have : x=1 1. x=1*1=1 (x++ --> x=2) 2. x=2*2=4 (x++ --> x=5) 3. x=5*5=25 (x++ --> x=26) x=26>6 so the loop stops and 26 is printed they used %d because it's a formatted print like the C language 'printf'. and '%d' is for integer. look here https://www.geeksforgeeks.org/formatted-output-in-java/
14th Apr 2020, 7:40 AM
John Robotane
John Robotane - avatar
0
Ok, thank you, understand it
14th Apr 2020, 7:50 AM
Umidbek
Umidbek - avatar