How to find Factorial of a number in Python
In this article, we will learn to find the factorial of a number.
The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers, and the factorial of zero is one.
Recursive Method:
# factorial of a number # recursive method def fact(n): if (n == 1 or n ==0): return 1 else: return n * fact(n - 1) n = 6 print("Factorial of ", n," is ", fact(n) )
Iterative Method:
# factorial of a number # iterative method def fact(n): if n < 0: return 0 elif n == 0 or n == 1: return 1 else: f = 1 while(n > 1): f *= n n -= 1 return f n = 6 print("Factorial of ", n," is ", fact(n) )
Subscribe
Login
Please login to comment
0 Discussion