Algorithm
1. Start
2. Read the input number (num)
3. Initialize a variable to store the reversed number (reverse_num) to 0
4. While num is not equal to 0:
a. Extract the last digit of num using num % 10 and store it in a variable (digit)
b. Multiply reverse_num by 10
c. Add digit to reverse_num
d. Update num by removing the last digit using num // 10
5. Display the reversed number (reverse_num)
6. End
Code Examples
#1 Code Example- Python program Reverse a Number using a while loop
Code -
Python Programming
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))
Copy The Code &
Try With Live Editor
Output
#2 Code Example - Pythom Program Using String slicing
Code -
Python Programming
num = 123456
print(str(num)[::-1])
Copy The Code &
Try With Live Editor
Output
Demonstration
Python Programing Example to Reverse a Number-DevsEnv