Reverse-a-Number-in-Python

Program to Reverse a Number in Python

In this post,  I will explain how to make a Python program which will revere a number i.e. when input number is  325 the output would be 525.


num =325
rnum= 0
while num != 0:
      digit = num % 10
      rnum = rnum*10+digit
      num =num//10
print(rnum)

If you see the above code first line  is num=325 i.e num refers 325.

Second line is rnum=0 i.e rnum refers 0.

The third line is a while loop which  does the main task.

Inside while loop, there are three statements

digit = num % 10
rnum = rnum*10+digit
num =num//10

It checks whether num not equal to zero.

If num is not equal to zero control will enter inside while loop and if it is equal to zero control will exit from while loop.

print(rnum) statement will print the reversed number.

To see line by line execution  see the video below.

 

 

 

 

 

 

 

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

©Postnetwork-All rights reserved.