# Reverse a Number in Python Using Recursion

(Last Updated On: 26/09/2023)

In this blog post, we will explore an easy approach to reverse a number in Python using recursion. We will also provide a step-wise implementation guide so that you will gain valuable insights into the underlying principles of recursion and its practical usage.

In programming, there are multiple techniques and algorithms to transform data arrangement. One interesting operation is reversing a number, where the digits of a given number are rearranged in the opposite order. Reversing a number can have practical applications in areas such as cryptography, data analysis, and even simple mathematical operations.

What is recursion in Python?

Recursion in Python is a programming technique where a function calls itself to solve a problem by breaking it down into smaller, similar subproblems. It is a powerful concept that allows a function to repeat its execution with different inputs until a specific condition is met.

So, let’s jump right in and explore the Python program to reverse a number using recursion.

## Python Program to Reverse a Number Using Recursion

```# Reverse a Number in python Using recursion

def reverse(n, r):
if n==0:
return r
else:
return reverse(n//10, r*10 + n%10)

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

# we read number from user and then pass this number to recursive function reverse()
reversed_number = reverse(number,0)

# Display output
print("Reverse of %d is %d" %(number, reversed_number))```

#### Reverse a Number Explanation

• Defining the recursive function reverse(n, r)

The first step is to check if the input number n is equal to 0. If it is, then we return the accumulated reversed number r.

If n is not 0, the program calculates the remainder of n divided by 10, which gives the last digit of the number. Now multiply the accumulated reversed number r by 10 and add the last digit. Call the reverse function recursively with the integer division of n by 10 as the new n value and the updated reversed number as r.

• Accepting input from the user

In this step, we display a message asking the user to enter a number. This input is converted into an integer using the int() function and assigned to the variable number.

• Calling the reverse function

Next, we pass the number as the n argument and 0 as the initial value for the reversed number r. The returned value is then assigned to the variable reversed_number.

• Displaying the result

Upon completion, the program prints the original number and the reversed number using the %d format specifier in the print() function.

#### Output:

``````Enter number: 12345678
Reverse of 12345678 is 87654321``````

The program uses recursion to reverse the digits of a given number. It repeatedly divides the number by 10 to remove the last digit and appends it to the right of the reversed number. The process continues until all digits have been processed, resulting in the reversed number. The output of the program displays both the original number and its reversed counterpart.

When the user inserts ‘12345678’, the program returns the output as ‘87654321’.

### The alternate methods to reverse a number in Python are:

Using a Loop to reverse the number digit by digit. This approach involves iterating through the digits of the number, extracting them one by one, and building the reversed number.

Using String Manipulation: You can convert the number to a string and reverse it using the slicing or reversed() function, and then convert it back to an integer.

In the above program, we use the recursive method as it offers readability, simplicity. Being relatively concise and easier to understand compared to the loop-based solutions it enhances your understanding of recursion as a fundamental programming concept. However, it’s important to note that recursive solutions may not always be the most efficient in terms of performance and memory usage. So, it’s crucial to consider the specific requirements and limitations of your program when choosing this method.

In conclusion, the above code demonstrates how to reverse a number using recursion. The code effectively reverses the digits of the input number. The reverse function plays a crucial role in this process, recursively removing the last digit from the number and appending it to the reversed number until all digits have been processed.

It’s worth noting that while recursion provides an elegant solution, it may not always be the most efficient approach for larger numbers due to the potential for stack overflow. In such cases, an iterative approach using loops might be more suitable.

#### Some FAQs when we do reverse a number in Python

What will happen when I change the n value to 0 in the reverse function?

When the value of n becomes 0, it means that all the digits of the original number have been processed. At this point, the function returns the reversed number r.

Is there a limit to the size of the number that can be reversed using this code?

If the recursion depth exceeds this limit, a RecursionError will occur. However, the code should work fine with reasonable-sized numbers.

What happens if I enter a negative number?

The program will still reverse the digits of the number, regardless of whether it is positive or negative. The sign of the number will be preserved.

What happens if I enter a non-numeric value or a string?

The program will raise a ValueError because it expects a valid integer input. You need to enter a numeric value for the program to work correctly.

Can I use this program to reverse a sequence of digits within a number?

No, this program reverses the entire number as a whole. It does not selectively reverse specific digit sequences within the number.