C# help understanding reverse numbers | Sololearn: Learn to code for FREE!
Nouvelle formation ! Tous les codeurs devraient apprendre l'IA générative !
Essayez une leçon gratuite
+ 1

C# help understanding reverse numbers

int original, reverse = 0; Console.Write("Enter a Number: "); // input 123 for test original = int.Parse(Console.ReadLine()); while (original != 0) // condition to be met before stopping i.e. original = 0 { // 2nd pass: 12 != 0 = true, continues while loop // 3rd pass: 1 != 0 true, continues while loop // 4th pass: 0 != 0 false, stops while loop reverse *= 10; // equal to: reverse = reverse * 10; 0 * 10 = 0 = reverse // 2nd pass: 3 * 10 = 30 = reverse // 3rd pass: 2 * 10 = 20 = reverse reverse += original % 10; // equal to: reverse = reverse + original % 10; 0 + 123 = 123 / 10 remainder = int 3 = reverse // 2nd pass: 30 + 12 = 42 / 10 remainder 2 = reverse // 3rd pass: 20 + 1 = 21 / 10 remainder 1 = reverse original /= 10; // equal to: original = original / 10; 123 / 10 = int 12 = original } // 2nd pass: 12 / 10 = int 1 = original // 3rd pass: 1 / 10 = "int" 0 = original Console.WriteLine("Reverse of Entered Number is: " + reverse); // how does this output 123 rather than just 1 I can grasp at the concept of the previous values of reverse accumulating during the iterations of the loop but why doesnt WriteLine() also output the string "Reverse of Entered Number is: " 3 times? I know it somewhat happens if I enter it in the loop body but the reverse output is still: blah blah blah: 3 // I understand this why doesn't this happen when WL() is outside----> " " : 32 // should'nt this be 2? "

2nd Feb 2018, 12:38 AM
kideoh0jima
1 Réponse
0
Avoid commenting like this, codes are self explanatory lines if you want to say something about a snippet means add them on top of the method no need to comment through the lines
2nd Feb 2018, 2:24 AM
Sujith k sukumar
Sujith k sukumar - avatar