+ 1
I want to understand this problem
What is the output of this code? int sum=0; for(int k=16; k!=1; k/=2) { sum += k; } cout << sum;
2 Respostas
+ 2
sum=sum+k
sum=0+16
sum=16
Now k=8 still not equal to 1 so loop runs again
sum=sum+k
sum=16+8
sum=24
now k=4
sum=24+4
sum=28
now k=2
sum=28+2
sum=30
Now k=1 so k!=1 condition evaluates to false so loop stops running and 30 is printed
+ 2
You gonna ask every question? please try some on your own.
For hint:
For each iteration k is added to sum variable and , then k is divided by 2 until k != 1