Without converting the number to a string:

def reverse_number(n):
    r = 0
    while n > 0:
        r *= 10
        r += n % 10
        n /= 10
    return r

print(reverse_number(123))
Answer from Alberto on Stack Overflow
🌐
Reddit
reddit.com › r/learnpython › can anyone explain how to reverse a integer number?
r/learnpython on Reddit: Can anyone explain how to reverse a integer number?
February 1, 2022 -

Sorry if that sound like stupid, I'm just a newbie to programming

At first I thought it was easy and started writing code and I find myself with bunch of errors.

Then I came up with this:

number = 12345
reverse = str(number)[::-1]
print(reverse)

It's just cheating you can say, I converted integer into string and made that string reverse.

After a while I thought, I'm just stupid to code like this but then later I googled and I found this solution which is more complicated to understand:

number = 12345

reverse = 0
while number > 0:
  last_digit = number % 10
  reverse = reverse * 10 + last_digit
  number = number // 10

print(reverse)

Can anyone please explain what's going on here?

🌐
Programiz
programiz.com › python-programming › examples › reverse-a-number
Python Program to Reverse a Number
To understand this example, you should have the knowledge of the following Python programming topics: ... num = 1234 reversed_num = 0 while num != 0: digit = num % 10 reversed_num = reversed_num * 10 + digit num //= 10 print("Reversed Number: " + str(reversed_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 - A list reversal approach involves converting the integer into a list of its digits, reversing the order of the list, and then converting it back to an integer. This method utilizes Python’s built-in list manipulation capabilities, such as list() and reverse(), to efficiently reverse the digits.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-to-reverse-a-number
Python Program to Reverse a Number - GeeksforGeeks
November 18, 2025 - Recursion works by repeatedly removing the last digit from the number and building the reversed number during the returning phase. It is elegant but inefficient compared to slicing or loops. ... def rev_num(n): if n < 10: return n return int(str(n % 10) + str(rev_num(n // 10))) n = 987654 print(rev_num(n))
🌐
Javatpoint
javatpoint.com › how-to-reverse-a-number-in-python
How to reverse a number in Python - Javatpoint
How to reverse a number in Python with python, tutorial, tkinter, button, overview, entry, checkbutton, canvas, frame, environment set-up, first python program, basics, data types, operators, etc.
🌐
Medium
medium.com › @reza.shokrzad › reverse-integer-problem-exploring-simple-and-optimized-python-solutions-8e6d6818c2bd
Reverse Integer Problem: Exploring Simple and Optimized Python Solutions | by Reza Shokrzad | Medium
June 14, 2024 - This problem tests a candidate’s ... to the Reverse Integer problem involves converting the integer into a string, reversing that string, and then converting it back to an integer....
Find elsewhere
🌐
w3resource
w3resource.com › python-exercises › challenges › 1 › python-challenges-1-exercise-18.php
Python: Reverse the digits of an integer - w3resource
def reverse_integer(x): sign = -1 if x < 0 else 1 x *= sign # Remove leading zero in the reversed integer while x: if x % 10 == 0: x /= 10 else: break # string manipulation x = str(x) lst = list(x) # list('234') returns ['2', '3', '4'] lst.reverse() x = "".join(lst) x = int(x) return sign*x print(reverse_integer(234)) print(reverse_integer(-234))
🌐
Educative
educative.io › answers › how-to-reverse-a-given-number-in-python
How to reverse a given number in Python
Here’s the implementation of this string slicing approach in Python to reverse the number: ... Note: Here, we have provided an explanation of the code segment that differs from the previous implementation. ... Line 10: Convert the number to a string using the str() function so we can reverse it using slicing. Line 11: Reverse the string using slicing [::-1]. Line 14: Convert the reversed string back to an integer using the int() function.
🌐
TutorialsPoint
tutorialspoint.com › reverse-integer-in-python
How to reverse a number in Python?
def reverse(num): st=str(num) revst=st[::-1] ans=int(revst) return ans num=12345 print(reverse(num))
🌐
Scaler
scaler.com › home › topics › reverse a number in python
Reverse a Number in Python - Scaler Topics
June 21, 2024 - In this method, we utilize the ... So, we just add the digits of our number into a list using the list() function in Python, then use the reverse method on it to get the digits in reverse order ......
🌐
Code and Debug
codeanddebug.in › home › data structures & algorithms › leetcode #7 : reverse integer python program explained
Leetcode #7 : Reverse Integer Python Program Explained
July 7, 2025 - Objective: The code aims to solve the problem of reversing the digits of a 32-bit signed integer “x”. Purpose: The function reverse is designed to take an integer “x”, reverse its digits, and return the reversed integer.
🌐
LeetCode
leetcode.com › problems › reverse-integer
Reverse Integer - LeetCode
Reverse Integer - Given a signed 32-bit integer x, return x with its digits reversed. If reversing x causes the value to go outside the signed 32-bit integer range [-231, 231 - 1], then return 0. Assume the environment does not allow you to store ...
🌐
datagy
datagy.io › home › python posts › python: reverse a number (3 easy ways)
Python: Reverse a Number (3 Easy Ways) • datagy
December 20, 2022 - Because of this, we can turn our ... at how we can accomplish this in Python: def reverse_number(number): """Reverses a number (either float or int) and returns the appropriate type....
🌐
Edureka
edureka.co › blog › how-to-reverse-a-number
How to reverse a number in Python | Python Program Explained | Edureka
September 24, 2019 - There are two ways to reverse a number in Python programming language – ... # 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))
🌐
C# Corner
c-sharpcorner.com › code › 3430 › reverse-number-in-python.aspx
Reverse Number In Python
Interview Question · WhatsApp · Sr Karthiga · Aug 06 2016 · 256.5k · 0 · 0 · reverse.zip · # Python Program to Reverse a Number using While loop · Number = int(input("Please Enter any Number: ")) Reverse = 0 · while(Number > 0): Reminder ...
🌐
PREP INSTA
prepinsta.com › home › python program › reverse a number in python
Reverse of a number in Python | PrepInsta​
July 29, 2023 - Using the formula reverse = ( reverse * 10 ) + remainder , we keep changing the reverse value. Break down the Nunber using divide operator. Print the reverse variable. Let’s implement the above mentioned Logic in Python Language.
🌐
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 - After extracting the last digit, we reduce the original number by dividing it by 10 (using integer division //). This removes the last digit, so in the first iteration, the number becomes 1234, then 123, and so on until it becomes 0. ... Finally, once the loop ends (when the number becomes 0), we print the reversed number, which in this case will be 54321. ... The while loop is an effective choice to reverse a number in Python using while loop because it allows you to perform repetitive tasks until a specific condition is met.