Python program to reverse a number with explanation

Posted by

In this post we are going to learn how to write a program to reverse the digits of a given number in Python programming language.

So Lets Start: 

Before directly moving on the writing reverse program in Python first you should understand the logic behind it.

Suppose if someone gives input 123 and our program should give output 321.

Logic behind it is, first our target is to take last number and store it and then second last and append it with previous one and then rest.

Python program to reverse a number

# Reverse A Number In Python

N=123

ans=0
while N!=0:
    ans=ans*10+N%10
    N=N//10

print(ans)

Leave a Reply

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