0

I just cant figure how the output in the following code becomes 121. Kindly help me get it.

number = 121 reverseNum = 0 while (number > 0): reminder = number % 10 reverseNum = (reverseNum * 10) + reminder number = number // 10 print (reverseNum )

19th Dec 2019, 5:09 PM
Dickson
Dickson - avatar
2 odpowiedzi
+ 2
Here is the code with additional print for debugging, including the result. number = 129 reverseNum = 0 while (number > 0): reminder = number % 10 reverseNum = (reverseNum * 10) + reminder number = number // 10 print(f'remainder={reminder} - revnum={reverseNum} - number={number}') print (reverseNum ) ''' number = 129 1. iteration: 129%10: remainder=9 - revnum=9 - number=12 2. iteration: 12%10: remainder=2 - revnum=92 - number=1 3. iteration: 1%10: remainder=1 - revnum=921 - number=0 result = 921 '''
19th Dec 2019, 8:40 PM
Lothar
Lothar - avatar
+ 1
Thank you both. You've been very helpful. I now get the logic!
19th Dec 2019, 9:03 PM
Dickson
Dickson - avatar