+ 2

How it's possible to output 256? Anyone can tell me.

int n=2, m=3; while(m--) n*=n; printf("%d",n);

10th Sep 2020, 7:52 PM
Suparna Podder
Suparna Podder - avatar
6 odpowiedzi
+ 5
M=3....n=2*2=4 M=2 ...n= 4*4=16 M=1...n= 16*16=256 Stop
10th Sep 2020, 8:00 PM
Oma Falk
Oma Falk - avatar
+ 5
m-- means the present value of m will be considered and then it will get decremented! First loop: Checking conditon: m = 3 ✅ Executing the block: n = n*n => 2*2 => 4 Second loop: Checking conditon: m = 2 ✅ Executing the block: n = n*n => 4*4 => 16 Third loop: Checking conditon: m = 1 ✅ Executing the block: n = n*n => 16*16 => 256 First loop: Checking conditon: m = 0 ❌ (0 and false are both falsy) Loop breaks! Final value of n = 256
10th Sep 2020, 8:01 PM
Namit Jain
Namit Jain - avatar
+ 3
Loop stops when value is decreased to 0 and 0 is evaluated as false which stops further execution of while loop
10th Sep 2020, 8:06 PM
Abhay
Abhay - avatar
+ 3
First read about loops and conditions you will understood much better . Always do dry run in rough copy and write all Values how it changing. Output will be 256 because int n =2 ,m=3 in while loop you wrote m While(m--) when first time loop will run it will be while(3--) here value will be decrease after that n*=n means n=2*2 (here n=2) Now n=4 then loop will run again m will be 2 now then n*=n ,n=4*4, n=16 then again loop will run and now value of m will be 1 so again n*=n , n=16*16=256 then m-- here m will be 0 so final answer 256 will print on screen You asked same question before in cpp but their m was m =2 but u asking how answer was 256 . https://www.sololearn.com/Discuss/2458725/?ref=app
11th Sep 2020, 1:30 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 1
While m-- means as long as m >0
11th Sep 2020, 7:03 AM
Oma Falk
Oma Falk - avatar
0
Oma Falk here mention not any condition that m value 3 is decreasing upto 1, so how l'm decide that?
10th Sep 2020, 8:03 PM
Suparna Podder
Suparna Podder - avatar