Reverse a number(integer) in python | Loop or Recursion

  • by

Numbers can be reversed in Python using different methods. Like loop or Recursion can use to reverse a number in python. let us take a look at the Python program to implement the same.

Ways to reverse a number in Python

  • Using a Loop
  • Using Recursion

Python programs to reverse a number

Let’s see the both examples:

Reverse a Number(integer) using Loop

Example of reverse a user input Number using While loop:

num = int(input("Enter your number: "))

rev_num = 0

while (num > 0):
    remainder = num % 10
    rev_num = (rev_num * 10) + remainder
    num = num // 10

# Display the result
print("The reverse number is : {}".format(rev_num))


Reverse a number(integer) in python

Reverse a Number(integer) using Recursion

Python Program to Reverse a user given Number using Recursion.

Num = int(input("Please Enter any Number: "))

Result = 0

def Result_Int(Num):
    global Result
    if (Num > 0):
        Reminder = Num % 10
        Result = (Result * 10) + Reminder
        Result_Int(Num // 10)
    return Result

Result = Result_Int(Num)
print("Reverse of entered number is = %d" % Result)


Reverse a Number(integer) using Recursion

Shortest Way

But it will not work with negative integer (numbers).

a = 123


Output: 321

Do comment if you know any other method or doubts on this tutorial.

IDE: PyCharm 2020.1.1 (Community Edition)
macOS 10.15.4
Python 3.7
All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.