This should work:

from math import log10
def rev(num):
    if num < 10:
        return num
    else:
        ones = num % 10
        rest = num // 10
        #print ones, rest, int(log10(rest) + 1), ones * 10 ** int(log10(rest) + 1)
        return ones * 10 ** int(log10(rest) + 1) + rev(rest)
print rev(9000), rev(1234), rev(1234567890123456789)

You could also reduce the number of times you call log10 and number of math operations by using a nested recursive function:

def rev(num):
    def rec(num, tens):
        if num < 10:
            return num        
        else:
            return num % 10 * tens + rec(num // 10, tens // 10)
    return rec(num, 10 ** int(log10(num)))
Answer from John Gaines Jr. on Stack Overflow
🌐
Tutorial Gateway
tutorialgateway.org › python-program-to-reverse-a-number
Python Program to Reverse a Number
April 7, 2025 - Number = int(input("Please Enter any Number: ")) Reverse = 0 while(Number > 0): Reminder = Number  Reverse = (Reverse *10) + Reminder Number = Number //10 print("\n Reverse of entered number is = %d" %Reverse) This program allows the user to ...
🌐
Newtum
blog.newtum.com › reverse-a-number-in-python-using-recursion
Reverse a Number in Python Using Recursion - Newtum
April 25, 2024 - Now multiply the accumulated reversed number r by 10 and add the last digit. Call the reverse function recursively with the integer division of n by 10 as the new n value and the updated reversed number as r.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-reverse-a-number
Python Program to Reverse a Number - GeeksforGeeks
November 18, 2025 - Explanation: The loop adds each character at the beginning of rev, forming the reversed order. Recursion works by repeatedly removing the last digit from the number and building the reversed number during the returning phase.
🌐
PREP INSTA
prepinsta.com › home › python program › reversing a number using recursion in python
Reversing a Number using Recursion in Python | PrepInsta | Python
October 12, 2022 - #reverse of a number using recursion def rev(num,ans=0): if num==0: return ans else: return rev(num//10,ans*10+(num))
🌐
PYnative
pynative.com › home › python › programs and examples › python programs to reverse an integer number
Python Reverse a Number [4 Ways] – PYnative
March 31, 2025 - num = 123456 reversed_int = 0 negative ... use the same mathematical formula discussed in the above approach, where we extract digits one by one and construct the reversed number in each recursive call....
🌐
DataFlair
data-flair.training › blogs › python-program-on-reverse-a-number-using-recursion
Python Program on Reverse a Number Using Recursion - DataFlair
February 28, 2024 - At its core, the program defines the reverse(n) function, which ingeniously employs recursion to calculate the reverse of a given number ‘n’. Notably, it introduces a global variable ‘s’, strategically utilized to store the reversed number.
Find elsewhere
🌐
Wikitechy
wikitechy.com › tutorials › python › python-program-to-reverse-a-number
[100% Working Code] - Python Program to Reverse a Number - python tutorial - Wikitechy
When the compiler reaches to Reverse = Reverse_Integer (Number) line in the program then the compiler will immediately jump to below function: ... In this function, below statement will help to call the function Recursively with updated value. If you miss this statement then, after completing ...
🌐
NxtWave
ccbp.in › blog › articles › reverse-a-number-in-python
Reverse a Number in Python: Methods & Best Practices
Start with a function with two parameters: number to reverse (num) and an accumulator (reversed_num) initially equal to 0. ... Extract the last digit of num using the modulo operator (num % 10).
🌐
EyeHunts
tutorial.eyehunts.com › home › reverse a number(integer) in python | loop or recursion
Reverse a number(integer) in python | Loop or Recursion - EyeHunts
August 25, 2021 - Num = int(input("Please Enter any ... Result_Int(Num // 10) return Result Result = Result_Int(Num) print("Reverse of entered number is = %d" % Result) ......
🌐
Sanfoundry
sanfoundry.com › python-program-reverse-given-number
Reverse a Number in Python - Sanfoundry
June 21, 2023 - ... The program takes a number as input and reverses it using recursion. In this case, when the input number is 12345, the output is 54321, which is the reversed form of the input number.
🌐
Python Guides
pythonguides.com › reverse-a-number-in-python
How To Reverse A Number In Python?
March 20, 2025 - Read How to Download and Extract ZIP Files from a URL Using Python? For those who appreciate elegant, recursive solutions: def reverse_number_recursive(number, reversed_num=0): """ Reverse a number using recursion. Args: number: The number to reverse reversed_num: The partially built reversed ...
🌐
Scaler
scaler.com › home › topics › reverse a number in python
Reverse a Number in Python - Scaler Topics
June 21, 2024 - In this way, we can reverse the string by calling the recursive function first and adding the character later. ... To reverse a number by this method, we keep adding the characters from the back of the original string to a new string using a for loop.
🌐
Upgrad
upgrad.com › home › tutorials › software & tech › reverse a number in python
How to Reverse a Number in Python: 5 Smart Ways with Code
November 11, 2024 - We call the reverse_number function recursively, passing the integer division of n by 10 (n // 10) to remove the last digit and continue the process until the base case is met.
🌐
CodeScracker
codescracker.com › python › program › python-program-find-reverse-of-number.htm
Python Program to Reverse a Number
This program uses a user-defined function named rev(), that returns reverse of a number passed as its argument. def rev(n): r = 0 while n!=0: r = (n) + (r*10) n = int(n/10) return r print("Enter a Number: ", end="") num = int(input()) print("\nReverse of " +str(num)+ " is " +str(rev(num))) ...
🌐
WsCube Tech
wscubetech.com › resources › python › programs › reverse-number
Reverse a Number in Python (5 Different Ways)
October 29, 2025 - Explore 5 different ways to reverse a number in Python. Get step-by-step code examples, outputs, and clear explanations to enhance your understanding.
Top answer
1 of 4
1

I'm not going to give you the answer, but I'll give some hints. It looks like you don't want to convert it to a string -- this makes it a more interesting problem, but will result in some funky behavior. For example, reverseDisplay(100) = 1.

However, if you don't yet have a good handle on recursion, I would strongly recommend that you convert the input to a string and try to recursively reverse that string. Once you understand how to do that, an arithmetic approach will be much more straightforward.

Your base case is solid. A digit reversed is that same digit.

def reverseDisplay(n):
    if n < 10:
        return n
    last_digit = # ??? 12345 -> 4
    other_digits = # ??? You'll use last_digit for this. 12345 -> 1234
    return last_digit * 10 ** ??? + reverseDisplay(???)
    # ** is the exponent operator. If the last digit is 5, this is going to be 500...
    # how many zeroes do we want? why?

If you don't want to use any string operations whatsoever, you might have to write your own function for getting the number of digits in an integer. Why? Where will you use it?


Imagine that you have a string 12345.

reverseDisplay(12345) is really 
    5 + reverseDisplay(1234) ->
        4 + reverseDisplay(123) ->
            3 + reverseDisplay(12) ->
                2 + reverseDisplay(1) ->
                    1
2 of 4
1

Honestly, it might be a terrible idea, but who knows may be it will help:

  1. Convert it to string.
  2. Reverse the string using the recursion. Basically take char from the back, append to the front.
  3. Parse it again.

Not the best performing solution, but a solution...

Otherwise there is gotta be some formula. For instance here: https://math.stackexchange.com/questions/323268/formula-to-reverse-digits

🌐
Vultr
docs.vultr.com › python › examples › reverse-a-number
Python Program to Reverse a Number | Vultr Docs
December 9, 2024 - This recursive function continues to call itself, each time reducing the number by a factor of ten and adjusting the reversed number accordingly. The recursion stops when the number becomes zero, and at this point, the completely reversed number is returned. Reversing a number in Python can be achieved through multiple approaches, each with its unique style and use case.
🌐
PREP INSTA
prepinsta.com › home › python program › reverse a number in python
Reverse of a number in Python | PrepInsta​
July 29, 2023 - Given an integer input number, we perform the following, Define a recursive function recursum() that takes in number and reverse variable as arguments. Set the base case as number == 0 and step recursive call as recursum(num/10, reverse).
🌐
Medium
medium.com › edureka › reverse-a-number-6eeb7eed0309
How to reverse a number in Python? | Edureka
February 4, 2021 - There are two ways to reverse a number in Python programming language - Using a Loop · Using Recursion · # Get the number from user manually num = int(input("Enter your favourite number: ")) # Initiate value to null test_num = 0 # Check using while loop while(num>0): #Logic remainder = num % 10 test_num = (test_num * 10) + remainder num = num//10 # Display the result print("The reverse number is : {}".format(test_num)) Output: Program Explanation ·