Reverse a Number in Python Using While Loop

(Last Updated On: 26/09/2023)

In this blog, we will explore how to reverse a number in Python using while loop. The while loop provides an elegant and efficient way to iterate through the digits of a number and construct its reversed counterpart.

Reversing a number is not only a fascinating programming exercise but also a practical problem that finds applications in various domains such as cryptography and numerical computations.

Here, we will provide a step-by-step logic and operations of a Python Program to Reverse a Number Using a While Loop. By understanding the inner workings of the program, you will gain insights and get familiar with the concepts that will enable you to apply this technique to your own projects and problem-solving scenarios.

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))

Code Explanation:

  • Accepting the user input

First, we prompt the user to enter a number which is then converted from a string to an integer using the int() function and stored in the variable num.

  • Initializing variables

Next, the variable reversed_num is initialized to 0. This variable will store the reversed number for the next step.

  • Reversing the number using a while loop

In this step, the while loop is executed as long as variable ‘num’ is not equal to 0.

Inside the loop, the remainder of num divided by 10 is calculated and stored in the variable ‘digit’. This gives us the rightmost digit of the number.

The current digit is then added to the reversed number by multiplying reversed_num by 10 and adding the digit to it.

The variable num is updated by performing integer division by 10 which removes the rightmost digit from num in each iteration.

The loop continues until all the digits of the original number are processed.

  • Displaying the reversed number

After the while loop terminates, the reversed number stored in reversed_num is converted to a string using the str() function. This reversed number is then concatenated with the string “Reversed Number: ” and displayed as the output.


Enter the number:1234567
Reversed Number: 7654321

In this program when the user inputs the value: 1234567 the program then reverses the number using the while loop. Each digit is extracted from the rightmost position using the modulo operator (%) and added to the reversed number. The loop continues until all the digits are processed. Finally, the reversed number is displayed as the output: 7654321.

Other ways to reverse a number in Python are:

  • Using String Manipulation which uses string slicing techniques to reverse the string.
  • Using the Recursion function that takes the number as an argument. It extracts the rightmost digit, recursively reverse the remaining number, and concatenates the reversed number with the extracted digit.

We used a while loop to reverse the number in the above program as it is straightforward and easy to understand. It involves basic arithmetic operations and does not require additional concepts like string manipulation or recursion.

The while loop method is also quite efficient in terms of time complexity as it iterates through the digits of the number once and performs constant-time operations. The while loop method allows you to easily modify the code to perform additional operations during the reversal process, such as calculating the sum or product of digits.

Some FAQs on reversing a number in Python

How does the program extract the rightmost digit of the number?

The rightmost digit of the number is obtained by calculating the remainder of the number divided by 10 using the modulo operator (%). This operation gives us the digit at the ones place.

How does the program remove the rightmost digit from the number in each iteration?

After extracting the rightmost digit, the program updates the number by performing an integer division by 10 (num //= 10). This removes the digit at the one’s place, effectively shifting the number to the right.

Can the program reverse negative numbers?

Yes, the program can reverse negative numbers. It treats the negative sign as a part of the number and reverses the digits accordingly. For example, -12345 would become -54321 when reversed.

What happens if I input a non-integer value?

If a non-integer value is entered, the program will raise a ValueError. The int() function used to convert the input expects a valid integer value, and any other input would result in an error.

Can I reverse a decimal or floating-point number using this program?

No, this program is specifically designed to reverse whole numbers. It does not handle decimal or floating-point numbers. To reverse a decimal number, you would need to convert it to an integer first and then apply the program.

Leave a Reply

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