Calculate Factorial using Recursive and Iterative Methods?
Run the code here: https://repl.it/@VinitKhandelwal/factorial
120
Run the code here: https://repl.it/@VinitKhandelwal/factorial
# Recursive Method - O(n)
def factorialRecursive(num):
if num == 2:
return 2
return num * factorialRecursive(num-1)
# Iterative Method - O(n)
def factorialIterative(num):
temp = num
facto = 1
for i in range(2, temp+1):
facto *= i
return facto
print(factorialRecursive(5))
print(factorialIterative(5))
OUTPUT
120120
Comments
Post a Comment