0
0
PythonProgramBeginner · 2 min read

Python Program to Reverse a Number Using While Loop

You can reverse a number in Python using a while loop by extracting the last digit with num % 10, adding it to a reversed number multiplied by 10, and then removing the last digit from the original number with num //= 10 until the number becomes zero.
📋

Examples

Input1234
Output4321
Input1000
Output1
Input7
Output7
🧠

How to Think About It

To reverse a number, think of peeling off its last digit repeatedly and building a new number from these digits in reverse order. Use a while loop to keep doing this until the original number is zero. Each time, get the last digit using num % 10, add it to the reversed number after shifting the reversed number left by one digit (multiply by 10), and then remove the last digit from the original number using num //= 10.
📐

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 modulus 10.
5
Multiply reversed number by 10 and add the last digit.
6
Remove the last digit from the input number by integer division by 10.
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 the number 1234 through the code to see how it reverses.

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 is now 0, exit loop

numdigitreversed_num
123444
123343
122432
114321
💡

Why This Works

Step 1: Extract last digit

Using num % 10 gets the last digit of the number, which we add to the reversed number.

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 remove the last digit from the original number and continue the loop.

🔄

Alternative Approaches

String conversion
python
num = input("Enter a number: ")
reversed_num = int(num[::-1])
print("Reversed number:", reversed_num)
This method is simpler and uses string slicing but does not use a while loop as requested.
Recursive function
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 instead of a loop; elegant but less intuitive for beginners.

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

Time Complexity

The loop runs once for each digit in the number, so the time complexity is O(d), where d is the number of digits.

Space Complexity

Only a few variables are used regardless of input size, so space complexity is O(1).

Which Approach is Fastest?

The while loop method is efficient and uses constant space. String conversion is simpler but uses extra space for the string. Recursion uses more stack space and is less efficient.

ApproachTimeSpaceBest For
While loopO(d)O(1)Memory-efficient, beginner-friendly
String conversionO(d)O(d)Quick and simple for small inputs
RecursionO(d)O(d)Elegant but uses more memory
💡
Always use integer division // to remove digits when working with numbers in loops.
⚠️
Beginners often forget to update the original number inside the loop, causing an infinite loop.