Can we reverse a number in python?
Example 1: Reverse a Number using a while loop
Output Show
4321 In this program, while loop is used to reverse a number as given in the following steps:
Example 2: Using String slicing
Output 654321 Using the string slicing concept, you can get reverse the string. Learn how to reverse a number in python. Reversing a number means rearranging the digits in a number such that the last digit comes first, followed by the second last digit and so on, and the first digit is at the end. There are numerous ways to reverse a number in python, which are discussed below. We are
given a number that has to be reversed. That means we need the last digit to come first, followed by the second last digit and so on, and the first digit will come last. One way of achieving this is by using the while loop. The condition for the while loop will be till our original number has non-zero digits left, i.e., till the original number is not equal to zero. We will initialize a new variable that will store our reversed number, so we need to add the last digit
first in this number. In the while loop, we can take the modulo of the number with ten to get the last digit. The answer to this will be the last digit. Now we need to add this digit to our reversed number, but it is possible that the reversed number already contains some digits. If we simply add this digit, it will result in the wrong answer. So we first need to shift all existing digits in the reversed number by one place. Hence, we multiply the reversed number with 10 and
add this digit. The current last digit of the original number has been added to the reversed number and hence should be removed. So, we divide the original number by 10 and take its floor, so the last digit is removed. The process continues until our original number becomes 0, and we get the reversed number in our variable. Code:
Output:
Reversing a number using String-SlicingTo reverse a number by this method, we use the property of strings, which is string slicing. Slicing is obtaining the substring of a string in python. The exciting thing about slicing is that if we use -1 as a parameter, we can obtain the substring from the end, thus reversing the number. Hence, in this method, we first convert our number to a string, and then by slicing, we reverse it. Code:
Output:
Reversing a number using RecursionTo reverse a number by this method, we use recursion. The thing with recursion is that we can keep calling the recursive function again and again till we reach the last index. This will be our base case, where we can then start adding the characters to the resultant reversed string. So, the characters will be added this way: the last index followed by the second last index and so on, and the first index will be added at the end. In this way, we can reverse the string by calling the recursive function first and adding the character later. Code:
Output: Reversing a number using for loopTo reverse a number by this method, we keep adding the characters from the back of the original string to a new string using a for a loop. In this way, the new string so formed is reversed, as the last character is added first to it, followed by the second last, and so on and the first character is added last. Code:
Output:
Reversing a number using reversed MethodWe use the python reversed() function to reverse a number by this method. This function returns a pointer that can iterate the string/list in reverse order. We use the join() method of python to obtain the reversed number from this iterator finally. Code:
Output:
Reversing a number using the ListIn this method, we utilize the fact that python provides a reverse() method for lists by which the elements of a list get reversed. So, we just add the digits of our number into a list using the list() function in python, then use the reverse method on it to get the digits in reverse order and then by using the join() method, we obtain the final reversed number. The join() method in python is used to convert an iterator into one single string. To obtain the reversed string from this iterator, we use the join() method of python and store the output in reversed_num. Code:
Output:
Reversing a number using the StackThis method utilizes the property of the stack. A stack stores elements in the LIFO or Last In First Out order. This means that the elements that are pushed at the end of the stack are the ones that are removed first. So, intuitively if we push all the digits of our number from the start into the stack, when popping them out, they will be in the reverse order, and we can store them to get the final reversed number. Code:
Output:
Conclusion
Can you reverse a number in Python?We use the python reversed() function to reverse a number by this method. This function returns a pointer that can iterate the string/list in reverse order. We use the join() method of python to obtain the reversed number from this iterator finally.
How do you reverse a number sequence in Python?Reverse Number In Python. # Python Program to Reverse a Number using While loop.. Number = int(input("Please Enter any Number: ")). Reverse = 0.. while(Number > 0):. Reminder = Number %10.. Reverse = (Reverse *10) + Reminder.. Number = Number //10.. print("\n Reverse of entered number is = %d" %Reverse). How do you reverse a number in Python without loops?Reverse a Number in Python Without using Loop
To reverse a number we need to extract the last digit of the number and add that number into a temporary variable with some calculation, then remove the last digit of the number. Do these processes until the number becomes zero.
How do you reverse data in Python?In Python, there is a built-in function called reverse() that is used to reverse the list. This is a simple and quick way to reverse a list that requires little memory. Syntax- list_name. reverse() Here, list_name means you have to write the name of the list which has to be reversed.
|