Reverse a Number in Python

Learn via video courses
Topics Covered

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.

Using while loop to Reverse a Number in Python

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 to 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 in Python using String-Slicing

To reverse a number in Python 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:

Using Recursion to Reverse a Number in Python

To reverse a number in Python 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 loop

To 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 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:

reverse a number using for loop method

Reversing a number using reversed Method

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.

Code:

Output:

Reversing a number using the List

In 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 Stack

This 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:

reverse a number using the stack method

Click Here, to learn about the pop() function in Python.

Conclusion

  • In this article, we learned how to reverse a given number in python. The article explored the various methods and their respective implementations that can be employed to reverse a number in python. Some involve changing the number into a string and using some string methods, while others involve while loop, for loop, recursion, or stacks.

Read More: