def factorial_iter(n):
product = 1
for i in range(n):
product = product * (i+1)
return product
def factorial_recursive(n):
if n == 1 or n == 0:
return 1
return n * factorial_recursive(n-1)
# f = factorial_iter(5)
f = factorial_recursive(0)
print(f)
def fact(n):
if n==0 or n==1:
return 1
else:
return n*fact(n-1)
print(fact(4)) #4 is the sample value it will returns 4!==> 4*3*2*1 =24
#OR
import math
print(math.factorial(4))
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
def factorial(n):
if n == 0:
return 1
return n * factorial(n-1)
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))
# This code is contributed by Smitha Dinesh Semwal
def factorial(n): # Define a function and passing a parameter
fact = 1 # Declare a variable fact and set the initial value=1
for i in range(1,n+1,1): # Using loop for iteration
fact = fact*i
print(fact) # Print the value of fact(You can also use "return")
factorial(n) // Calling the function and passing the parameter
#easy way to find factorial of number with while
b=1
a=int(input('the number to be entered'))
c=1
while c<=a:
b*=c
c+=1
print('factorial',a,'is',b)
#output:
the number to be entered x
factorial x is x!
+-+-+-+-+-+-+-+-+++-+-+-+-+-+-+++-+-+++-+++-+-++-+-A
# Python 3 program to find
# factorial of given number
# Function to find factorial of given number
def factorial(n):
res = 1
for i in range(2, n+1):
res *= i
return res
# Driver Code
num = 5;
print("Factorial of", num, "is",
factorial(num))