def factorial_recursive(n):
"""Calculates factorial of n recursively."""
if n < 0:
return "Factorial does not exist for negative numbers"
elif n == 0:
return 1
else:
return n * factorial_recursive(n-1)
# Example usage:
num = 5
print(f"The factorial of {num} is {factorial_recursive(num)}")
# Expected output: The factorial of 5 is 120
این تابع ابتدا شرایط پایه (اعداد منفی و صفر) را بررسی کرده و سپس به صورت بازگشتی فاکتوریل را محاسبه میکند.
- باباطاهر