How to Reverse Digits of a Number Using While Loop in Python – GATE, Interview & Placement Practice
164 views
Jun 1, 2025
Get the full source code of application here:
View Video Transcript
0:00
uh hello guys welcome to this video so
0:02
in this video I will show you a very
0:04
common program which is asked inside uh
0:06
coding interviews and competitive exams
0:09
such as GATE or placements which is how
0:12
to reverse a number inside Python so
0:16
this is actually essentially the program
0:18
right here if I execute this it will ask
0:22
you to enter a number you can write any
0:24
number let's suppose I write uh 5 6 7
0:29
this is the number that I write here so
0:33
what will be the output so what this
0:36
program will do it will reverse all the
0:38
digits of this number which output will
0:41
be
0:44
765 so as I enter it you will see
0:49
reverse of this number is 765 so how to
0:52
calculate the reverse i will show you in
0:54
this video this is a simple logic using
0:58
while loop we are using while loop here
1:01
so let me just paste this program in a
1:04
software called as Python tutor which
1:06
will show you step by step how this
1:09
program executes line by line so let me
1:11
paste it and visualize execution so now
1:15
the very first step right here it is
1:17
asking you to enter the number so if I
1:19
write here
1:21
45 6 and click on submit and you can see
1:25
we have declared a global variable which
1:27
is
1:28
456 now we need to reverse this number
1:31
and print out
1:33
654 this is our task so now to do this
1:37
we actually declare a brand new variable
1:40
which will be called as reverse and it's
1:44
in initialized to zero so now a new
1:47
variable has been declared which is
1:49
reverse which is set to zero and for
1:52
this we are having this while
1:55
loop we are using the while loop here
1:58
while n is greater than
2:01
zero in this scenario you will see n is
2:04
456 it is greater than zero so this
2:07
condition will evaluate to true and now
2:10
the control will shift to the center of
2:12
the loop
2:14
so the very first step right here what
2:17
we are doing we are dividing this number
2:19
by 10 whatever is the number which is 4
2:21
5
2:22
6 so we are dividing it by 10 and
2:26
whatever is the remainder comes out to
2:28
be so we know
2:33
that the remainder will be six
2:37
so what will be the output here which
2:39
will be stored inside this remainder
2:42
variable which will be six so now you
2:45
can
2:46
see now we extracted the last digit
2:49
right here and printed out in the front
2:52
so this is the way and now in the next
2:56
step
2:57
we update this value of
3:01
reverse and what we do right here we
3:03
divide uh sorry multiply this number by
3:06
10 and add whatever is the remainder
3:09
there
3:10
so right here reverse is 0 so 0 *
3:15
6 0 * 10 comes out to be 0 and whatever
3:20
is the remainder is 6 so its value will
3:24
also change to six so now if you see it
3:27
also becomes
3:29
six and now we divide this number by 10
3:33
so this division operator is different
3:36
this will not catch the remainder this
3:38
will only divide the number by 10 so if
3:40
you divide this by 10
3:44
again so it comes out to be 45 and this
3:48
six will be removed so now it becomes 45
3:53
now you can see that again we repeat
3:55
this process 45 is greater than zero
3:59
again we divide this number by 10
4:05
so we say 10 4 are 40 and we get
4:08
remainder once again of five it's a very
4:11
simple process how we extract the last
4:14
digit just divide the number by 10 and
4:16
get the remainder now we'll be shifting
4:19
this value to this one so again
4:22
we put this value now this will make the
4:27
remainder to five so again we do we
4:31
multiply
4:33
this reverse whatever is present we
4:37
multiply this by 10 so 6 10 are 60 and
4:41
then whatever is the remainder is there
4:43
which is 60 + 5 and it comes out to be
4:49
65 so as you can see so now once again
4:54
we divide this number by 10 so we remove
4:57
this so now this will be four again
5:02
control shift again four is greater than
5:04
zero again we divide this by 10 and the
5:08
remainder will be zero
5:11
now so now this will become four sorry
5:15
this will become four because if you
5:17
divide
5:20
by so you will
5:23
see remainder comes out to be four so
5:26
now it is four
5:29
so again now in the next
5:32
step we multiply this by 10 so it comes
5:36
out to be
5:37
650 +
5:40
4 so now the output is
5:43
654 so that's all we reverse the number
5:47
and uh
5:49
now n is we again divide this by
5:55
10 so it comes out to be zero and uh n
5:59
becomes zero so this condition will
6:01
evaluates to false so we will break out
6:03
from the loop and simply print out the
6:06
reverse number is
6:08
654 so this is a very simple three-step
6:11
logic if you want to reverse all the
6:13
digits inside the number first of all
6:15
you divide the number and catch the
6:17
remainder and then you multiply
6:21
remainder you multiply whatever is the
6:25
reverse so we declared this reverse
6:28
variable that's why we declared this to
6:31
store
6:32
this so I showed you step by step in
6:35
this way you can reverse the number
6:38
using while loop uh please hit that like
6:41
button subscribe the channel and also
6:44
check out my website freemediatools.com
6:48
uh which contains thousands of tools
#Programming