uh hello guys welcome to this video so
in this video I will show you a very
common program which is asked inside uh
coding interviews and competitive exams
such as GATE or placements which is how
to reverse a number inside Python so
this is actually essentially the program
right here if I execute this it will ask
you to enter a number you can write any
number let's suppose I write uh 5 6 7
this is the number that I write here so
what will be the output so what this
program will do it will reverse all the
digits of this number which output will
be
765 so as I enter it you will see
reverse of this number is 765 so how to
calculate the reverse i will show you in
this video this is a simple logic using
while loop we are using while loop here
so let me just paste this program in a
software called as Python tutor which
will show you step by step how this
program executes line by line so let me
paste it and visualize execution so now
the very first step right here it is
asking you to enter the number so if I
write here
45 6 and click on submit and you can see
we have declared a global variable which
is
456 now we need to reverse this number
and print out
654 this is our task so now to do this
we actually declare a brand new variable
which will be called as reverse and it's
in initialized to zero so now a new
variable has been declared which is
reverse which is set to zero and for
this we are having this while
loop we are using the while loop here
while n is greater than
zero in this scenario you will see n is
456 it is greater than zero so this
condition will evaluate to true and now
the control will shift to the center of
the loop
so the very first step right here what
we are doing we are dividing this number
by 10 whatever is the number which is 4
5
6 so we are dividing it by 10 and
whatever is the remainder comes out to
be so we know
that the remainder will be six
so what will be the output here which
will be stored inside this remainder
variable which will be six so now you
can
see now we extracted the last digit
right here and printed out in the front
so this is the way and now in the next
step
we update this value of
reverse and what we do right here we
divide uh sorry multiply this number by
10 and add whatever is the remainder
there
so right here reverse is 0 so 0 *
6 0 * 10 comes out to be 0 and whatever
is the remainder is 6 so its value will
also change to six so now if you see it
also becomes
six and now we divide this number by 10
so this division operator is different
this will not catch the remainder this
will only divide the number by 10 so if
you divide this by 10
again so it comes out to be 45 and this
six will be removed so now it becomes 45
now you can see that again we repeat
this process 45 is greater than zero
again we divide this number by 10
so we say 10 4 are 40 and we get
remainder once again of five it's a very
simple process how we extract the last
digit just divide the number by 10 and
get the remainder now we'll be shifting
this value to this one so again
we put this value now this will make the
remainder to five so again we do we
multiply
this reverse whatever is present we
multiply this by 10 so 6 10 are 60 and
then whatever is the remainder is there
which is 60 + 5 and it comes out to be
65 so as you can see so now once again
we divide this number by 10 so we remove
this so now this will be four again
control shift again four is greater than
zero again we divide this by 10 and the
remainder will be zero
now so now this will become four sorry
this will become four because if you
divide
by so you will
see remainder comes out to be four so
now it is four
so again now in the next
step we multiply this by 10 so it comes
out to be
650 +
4 so now the output is
654 so that's all we reverse the number
and uh
now n is we again divide this by
10 so it comes out to be zero and uh n
becomes zero so this condition will
evaluates to false so we will break out
from the loop and simply print out the
reverse number is
654 so this is a very simple three-step
logic if you want to reverse all the
digits inside the number first of all
you divide the number and catch the
remainder and then you multiply
remainder you multiply whatever is the
reverse so we declared this reverse
variable that's why we declared this to
store
this so I showed you step by step in
this way you can reverse the number
using while loop uh please hit that like
button subscribe the channel and also
check out my website freemediatools.com
uh which contains thousands of tools