Recursive Factorial Calculation with Accumulator

  • Share this:

Code introduction


This function calculates the factorial of a non-negative integer, which is the product of all positive integers up to that number. It uses recursion, with an accumulator parameter `acc` for multiplication.


Technology Stack : Recursion

Code Type : Recursive function

Code Difficulty : Intermediate


                
                    
def factorial(n, acc=1):
    if n <= 1:
        return acc
    else:
        return factorial(n-1, n*acc)                
              
Tags: