I want to understand this problem | Sololearn: Learn to code for FREE!

+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;

8/30/2020 3:15:18 PM

Bayzid

2 Answers

New Answer

+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