Answers for "python recursion factorial"

2

factorial recursion python

def fact_rec(n):
	if n < 0:
		return
	elif n <= 1:
		return 1
	else:
		return n*fact_rec(n-1)

answer = fact_rec(4)
if answer == None: 
	print("Cannot calculate factorial of a negative value")
else:
	print(answer)  # 24 = 4x3x2x1 = 4!
Posted by: Guest on February-12-2022
0

python recursion factorial

def factorial(n):

    assert type(n) == int, "Invalid input type"
    assert n >= 0, "Input must be non-negative"
    
    if n <= 1:
        return n
    else:
        return n*factorial(n-1)
Posted by: Guest on March-15-2022

Python Answers by Framework

Browse Popular Code Answers by Language