Can you explain the following code, it reverses a no but I don’t know how it works?

n=12345 rev=0 while(n>0): dig=n%10 rev=rev*10+dig n=n//10 print("Reverse of the number:",rev)

9/7/2019 5:24:23 AM

Parvinder Rathore

7 Answers

New Answer


it takes the left digit using reminder operator then multiple it with 10 to add the next digit at the right usually I understand codes by printing the changes check this may help https://code.sololearn.com/c6Tgr225h1Ea/?ref=app




Parvinder Rathore I can tell you how it is working but did you tried debugging it yourself? Print the values of dig, rev and n after each iteration and see how it works. It is easy to understand. This is the most basic way of debugging.


Downvoting why? I am trying to teach the basics of debugging, but you want things to be spoon fed ! Best of luck for your programming journey.


This visual debugger is ideal for beginners: https://kutt.it/unekDH


thank you both of you for your support , I am new to coding , your help really counts ~ swim ~ thank you , I didn’t downvoted your answer intentionaly may be it was something in haste though I realised my mistake now . I would look forward for your help in future


Parvinder Rathore 👍🙂