Master Python with 70+ Hands-on Projects and Get Job-ready - Learn Python
In this article, we will explore a Python program that calculates the reverse of a number using recursion. The program highlights the use of recursion and a global variable to reverse the digits of a given number. Additionally, it checks if the reversed number is a palindrome, showcasing the versatility of recursive functions in solving more complex problems.
Prerequisites
- Basic understanding of functions.
- Familiarity with recursion.
- Knowledge of global variables in Python.
- Understanding of conditional statements in Python.
Topic Explanation
At its core, the program defines the reverse(n) function, which ingeniously employs recursion to calculate the reverse of a given number ‘n’. Notably, it introduces a global variable ‘s’, strategically utilized to store the reversed number. This article delves into the step-by-step workings of the code, illustrating how recursion plays a pivotal role in achieving the reversal.
he program extends beyond basic reversal, incorporating an additional layer of complexity by examining whether the original and reversed numbers together form a palindrome. It invites user interaction by requesting input for the target number, then proceeds to call the reverse() function, displaying the reversed number. This two-pronged approach showcases not only the elegance of recursion but also the practicality of applying it to intricate problem-solving scenarios.
Code:
# Reverse of a number using recursion
s = 0
def reverse(n):
"""
Recursive function to calculate the reverse of a number.
Parameters:
- n (int): The input number.
Returns:
- int: The reversed number.
"""
global s
if n > 0:
r = n % 10
s = s * 10 + r
n = n // 10
reverse(n)
return s
# Calling the reverse function
n = int(input("Enter a number: "))
x = reverse(n)
print("Reverse is %d" % x)
Output:
Enter a number: 45
Reverse is 54
Code Explanation:
1. Defines a global variable s initialized to 0
2. Defines a function reverse() that takes one integer parameter n
3. In the function:
I) Base case: If n <= 0, stop recurring
II) Get last digit: r = n % 10
III) Append r to s: s = s * 10 + r
- Builds the reverse number digit-by-digit
IV) Remove last digit: n = n // 10
V) Call reverse(n) again recursively
4. Return the global s which now contains the reversed number
5. Get user input for a number and store in n
6. Call reverse(n), store result in x
7. Print “Reverse is” along with the value of x
Summary
In conclusion, this Python program presents an instructive illustration of recursive techniques coupled with the use of a global variable to effectively reverse a user-provided number. It serves as a valuable educational resource for individuals with a foundational understanding of Python, offering a practical application of recursion and highlighting its versatility in problem-solving. By employing recursion to reverse the number and employing a global variable to store the result, the program provides a comprehensive learning experience, showcasing the elegance and efficiency of recursive algorithms.