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 )
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
'''
+ 1
Thank you both. You've been very helpful. I now get the logic!