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?
2 Respuestas
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/
0
Ok, thank you, understand it