Sure, here is a Python function to find the factorial of a given number using recursion:
Python
def factorial(number):
"""
Finds the factorial of a given number using recursion.
Args:
number: The number to find the factorial of.
Returns:
The factorial of the number.
"""
if number == 0:
return 1
else:
return number * factorial(number - 1)
if __name__ == "__main__":
number = 5
factorial_value = factorial(number)
print(factorial_value)
This function works by first checking if the number is 0. If it is, then the function returns 1. Otherwise, the function recursively calls itself, passing in the number minus 1. The function then multiplies the result of the recursive call by the number and returns the result.
To run the function, you can save it as a Python file and then run it from the command line. For example, if you save the function as
factorial.py, you can run it by typing the following command into the command line:
Code snippet
python factorial.py
This will print the factorial of the number to the console.
Here is an example of the output of the function:
Code snippet
$ python factorial.py
120
As you can see, the output of the function is 120, which is the factorial of 5.
Markdown for AI
A clean, structured version of this page for AI assistants and LLMs.
We use cookies to ensure you have the best browsing experience on our website. By using our site, you
acknowledge that you have read and understood our
Cookie Policy &
Privacy Policy.
Sure, here is a Python function to find the factorial of a given number using recursion:
Python
This function works by first checking if the number is 0. If it is, then the function returns 1. Otherwise, the function recursively calls itself, passing in the number minus 1. The function then multiplies the result of the recursive call by the number and returns the result.
To run the function, you can save it as a Python file and then run it from the command line. For example, if you save the function as
factorial.py, you can run it by typing the following command into the command line:Code snippet
This will print the factorial of the number to the console.
Here is an example of the output of the function:
Code snippet
As you can see, the output of the function is 120, which is the factorial of 5.