C# While Loop Question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

C# While Loop Question

Ok guys and gals, for some reason my brain is not processing this simple while loop. Can someone help me with why I'm getting an output of 5? static void Main(string[] args) { int i = 0; int a = 92751; while (a != 0){ a /= 10; i++; } Console.Write(i);

29th Jul 2017, 3:36 PM
Carl Jones
Carl Jones - avatar
2 Answers
+ 5
Yap. The Out put is 5, First 92751 != 0. So it gets divided by 10. i=1; Next 9275 != 0. So it gets divided by 10. i=2; Next 927 != 0. So it gets divided by 10. i=3; Next 92 != 0. So it gets divided by 10. i=4; Next 9 != 0. So it gets divided by 10. i=5; Next 0 = 0. Then Loop will exit Finally i = 5
29th Jul 2017, 4:00 PM
Yasiru Nayanajith
Yasiru Nayanajith - avatar
0
Ah! Thanks Yasiru!
29th Jul 2017, 9:56 PM
Carl Jones
Carl Jones - avatar