0
0
PythonProgramBeginner · 2 min read

Python Program to Reverse a Number with Output and Explanation

You can reverse a number in Python by repeatedly extracting its last digit using % 10 and building the reversed number by multiplying the current reversed number by 10 and adding the digit, like this: while num > 0: digit = num % 10; reversed_num = reversed_num * 10 + digit; num //= 10.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off its last digit one by one using the remainder operator %. Then, build a new number by shifting the current reversed number left (multiply by 10) and adding the peeled digit. Repeat until the original number is fully processed.
📐

Algorithm

1
Get the input number.
2
Initialize reversed number as 0.
3
While the input number is greater than 0:
4
Extract the last digit using remainder operator.
5
Add this digit to reversed number after shifting reversed number left by one digit.
6
Remove the last digit from the input number by integer division.
7
Return the reversed number.
💻

Code

python
num = int(input("Enter a number: "))
reversed_num = 0
while num > 0:
    digit = num % 10
    reversed_num = reversed_num * 10 + digit
    num //= 10
print("Reversed number:", reversed_num)
Output
Enter a number: 1234 Reversed number: 4321
🔍

Dry Run

Let's trace reversing 1234 through the code

1

Initial values

num = 1234, reversed_num = 0

2

First loop iteration

digit = 1234 % 10 = 4; reversed_num = 0 * 10 + 4 = 4; num = 1234 // 10 = 123

3

Second loop iteration

digit = 123 % 10 = 3; reversed_num = 4 * 10 + 3 = 43; num = 123 // 10 = 12

4

Third loop iteration

digit = 12 % 10 = 2; reversed_num = 43 * 10 + 2 = 432; num = 12 // 10 = 1

5

Fourth loop iteration

digit = 1 % 10 = 1; reversed_num = 432 * 10 + 1 = 4321; num = 1 // 10 = 0

6

Loop ends

num = 0, reversed_num = 4321

numdigitreversed_num
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, like peeling one digit off.

Step 2: Build reversed number

Multiply the current reversed number by 10 to shift digits left, then add the extracted digit to append it.

Step 3: Remove last digit

Use integer division num //= 10 to drop the last digit from the original number and continue.

🔄

Alternative Approaches

String conversion
python
num = input("Enter a number: ")
reversed_num = num[::-1]
print("Reversed number:", int(reversed_num))
This method is simpler and uses string slicing but converts number to string and back, which may be less efficient for very large numbers.
Recursive approach
python
def reverse_num(num, rev=0):
    if num == 0:
        return rev
    return reverse_num(num // 10, rev * 10 + num % 10)

num = int(input("Enter a number: "))
print("Reversed number:", reverse_num(num))
Uses recursion to reverse the number, elegant but may hit recursion limits for very large inputs.

Complexity: O(d) time, O(1) space

Time Complexity

The loop runs once for each digit in the number, so time grows linearly with the number of digits, O(d).

Space Complexity

Only a few variables are used, so space is constant, O(1).

Which Approach is Fastest?

The arithmetic loop method is fastest and uses constant space. String conversion is simpler but uses extra memory for strings. Recursion is elegant but less efficient.

ApproachTimeSpaceBest For
Arithmetic loopO(d)O(1)Efficient and memory-friendly
String conversionO(d)O(d)Simple code, small numbers
RecursiveO(d)O(d)Elegant but limited by recursion depth
💡
Use integer division // to remove digits and modulus % to extract digits when reversing numbers.
⚠️
Forgetting to update the original number inside the loop causes an infinite loop.