Recursive Factorial Calculation

  • Share this:

Code introduction


Calculates the factorial of a number, which is the product of all positive integers up to that number. This is implemented using a recursive function.


Technology Stack : Recursive function

Code Type : Function

Code Difficulty : Intermediate


                
                    
def factorial(n, accumulator=1):
    if n < 0:
        return "Factorial is not defined for negative numbers"
    elif n == 0:
        return accumulator
    else:
        return factorial(n-1, n*accumulator)