Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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! 
 
PREVIOUS NEXT
Tagged: #factorial #recursion #python
ADD COMMENT
Topic
Name
3+4 =