How this code execute ? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How this code execute ?

#include <stdio.h> int main() { int n=2,m=2; while(m--) n*=n; printf ("%d",n); return 0; }

7th Jan 2021, 1:29 PM
Shubham Maske
Shubham Maske - avatar
4 Answers
+ 5
Let me translate everything inside main() to english. 1) create 2 variables, one with name 'n'(initial value = 2) and another with name 'm'(initial value = 2). 2) loop till the time 'm' has a non-zero value (zero in C defaults to false) and decrement it after every iteration 3) inside the loop, square the value present in variable 'n' . So values inside loop would be For m == 2 n = 2*2 == 4 For m == 1 n = 4*4 == 16 Now value of 'm' is zero so break the loop. 4) print the value inside 'n' on the console which is 16
7th Jan 2021, 1:40 PM
Arsenic
Arsenic - avatar
+ 1
Now i get it Thanks buddy. Arsenic
7th Jan 2021, 1:44 PM
Shubham Maske
Shubham Maske - avatar
+ 1
Since, the value of m is 2. So, loop is executed 2 times because value of m is decreases by 1 after completing 1 loop. In first round n = 2*2 = 4. In second round n = 4*4 = 16. So, in output screen compiler shows 16. https://code.sololearn.com/cvZ6x2Kc7s7r/?ref=app
10th Jan 2021, 5:09 AM
Ashish Gupta
Ashish Gupta - avatar
0
10th Jan 2021, 7:21 AM
Shubham Maske
Shubham Maske - avatar