Python program to reverse a number with explanation

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

So Lets Begin: 

Before directly moving on the writing reverse program in C 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

n = int(input("please give a number : "))
print("before reverse your numeber is : %d" %n)
reverse = 0
while n!=0:
    reverse = reverse*10 + n%10       
    n = (n//10)
print("After reverse : %d" %reverse) 


program to reverse a number in python

Leave a Comment