Google News
logo
Python Program to Recursion
Recursion is a programming technique where a function calls itself repeatedly until a certain condition is met.

Here is an example Python program that demonstrates recursion :
Program :
def factorial(n):
    if n == 1:
        return 1
    else:
        return n * factorial(n - 1)

# Test the function
print(factorial(5))
Output :
120
In this example, the `factorial()` function calculates the factorial of a given number using recursion. The base case is when `n == 1`, which means that the function returns `1` when `n` is `1`.

For all other values of `n`, the function calls itself with `n - 1` as the argument, and multiplies the result by `n`. The recursion stops when `n` reaches `1`, and the final result is returned to the caller.