What is the output of the following code? int x = 64; int y = 0; while (x % 2 == 0) { y++; x = x / 2; } System.out.println(x + " " + y);
Answers
Answered by
8
let's take the value of x= 2^6=64(2 raised to power 6)
so in a while loop(after satifsy condition x%2==0) statement increase the value of y by 1 .
and x value divde by 2.
x=2^6, y=0
x=2^5, y=1
x=2^4, y=2
x=2^3, y=3
x=2^2, y=4
x=2^1, y=5
x=2^0=1, y=6
so while loop condition x%2==0
1%2==1:: not satisfy the condition.
therfore come out from while loop and execute next statement and print .
output: 1 6
so in a while loop(after satifsy condition x%2==0) statement increase the value of y by 1 .
and x value divde by 2.
x=2^6, y=0
x=2^5, y=1
x=2^4, y=2
x=2^3, y=3
x=2^2, y=4
x=2^1, y=5
x=2^0=1, y=6
so while loop condition x%2==0
1%2==1:: not satisfy the condition.
therfore come out from while loop and execute next statement and print .
output: 1 6
Similar questions
Math,
7 months ago
Computer Science,
7 months ago
Math,
7 months ago
Computer Science,
1 year ago
Math,
1 year ago
English,
1 year ago