+1

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

+7

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

+6

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.

+6

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.

+6

welcome

+3

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

+2

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

+2

Parvinder Rathore 👍🙂