#include <stdio.h> main() { int i; for(i=0;i<5;i+=4) { i=i*i; } printf("ans %d",i); return 0; } | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

#include <stdio.h> main() { int i; for(i=0;i<5;i+=4) { i=i*i; } printf("ans %d",i); return 0; }

Loop problem, in which how we get ans 20 .

11th Sep 2020, 9:44 AM
Chauhan Ashutoshkumar Harishankar
Chauhan Ashutoshkumar Harishankar - avatar
5 Answers
+ 6
Loop1: i = 0 i = i * i //still 0 i += 4 //from loop Loop2: i = 4 i = i * i //16 i += 4 //20
11th Sep 2020, 9:49 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 3
When i is 0 we will check condition i<5 (0<5) it true then i=i*i means 0*0 so i=0 then next time i will increase by 4 means i+=4 which is i=0+4 here i will be 4 then it will check condition i<5 and i is 4 4<5 condition true then i=i*i = 4*4 means 16 it will assign to i after that it will increase by 4 i+=i (i=16+4) which is 20 so we will again check condition i<5 and i is 20 20<5 which is false so last value will print 20 as a Output.
11th Sep 2020, 9:50 AM
A S Raghuvanshi
A S Raghuvanshi - avatar
+ 2
At i =0 we have i=0*0 ,i=0,then Edit: At i=4 we have i=4*4,i=16,then i+=4 and loop stops with i=16+4=20
11th Sep 2020, 9:49 AM
Abhay
Abhay - avatar
+ 1
Abhay answer is 20. Read your answer again please.
11th Sep 2020, 9:50 AM
Raj Chhatrala
Raj Chhatrala - avatar
+ 1
🔫 Rick Grimes just realised ty
11th Sep 2020, 9:50 AM
Abhay
Abhay - avatar