Python reverse integer using recursion
Question: I am working on a problem that need to reverse a integer input without using
list or string. But my program only return first and last digits.

    
    
    def reverseDisplay(number):
       if number<10:
          return number 
       else:
          return reverseDisplay(number//10)
    def main():
        number=int(input("Enter a number:"))
        print(number%10,end='')
        print(reverseDisplay(number))
    main()
    

It's seems like the reverse function just worked only once.


Answer: 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)))
    

