Reverse a Number in Python Using While Loop

(Last Updated On: 28/12/2022)

Python Program to Reverse a Number Using While Loop

# Reverse a Number in python using while Loop

# we are taking a number from user as input
# entered value will be converted to int from string   	 
num = int(input("Enter the number:"))

reversed_num = 0

while num != 0:
	# remainder of the num divided by 10 is stored in the variable digit
	digit = num % 10
	reversed_num = reversed_num * 10 + digit
	num //= 10

print("Reversed Number: " + str(reversed_num))

Output:

Enter the number:123456
Reversed Number: 654321

Leave a Reply

Your email address will not be published. Required fields are marked *