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.
Discover the step-by-step process of reversing a number using a While Loop in Python. Gain insights into the inner workings of the program, empowering you to apply this technique to various projects and problem-solving scenarios effectively.
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.
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:
Other methods to generate a reverse number in Python include using slicing, recursion, or converting to a string and reversing it.
- 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, iterating through the digits of the number once and performing constant-time operations, proves quite efficient in terms of time complexity. It also offers flexibility, enabling easy modification for additional operations like calculating the sum or product of digits during the reversal process.
We hope that our blog on ‘Reverse a number in Python using while loop’ provides a thorough understanding of the concept. For learning such more topics keep checking tabs on the Newtum website which keeps posting and providing Blogs and Courses related to programming languages.
Some FAQs on reversing a number in Python using while loop
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.
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.
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.
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.
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.