xxxxxxxxxx
#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
xxxxxxxxxx
import math
math.factorial(5) # Using math module
def factorial(n): # Doing it yourself
x = 1
for i in range(2,n+1):
x *= i
return x
xxxxxxxxxx
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))
xxxxxxxxxx
# 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
xxxxxxxxxx
def factorial(n):
fact = 1
for num in range(2, n + 1):
fact *= num
return fact
xxxxxxxxxx
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
# Example usage:
num = 5
result = factorial(num)
print(f"The factorial of {num} is: {result}")
xxxxxxxxxx
def Fact(num):
z=1
while(1):
z=z*num
num=num-1
if(num==0):
break
print(z)
Fact(4)
xxxxxxxxxx
def factorial(n)
if n < 2:
return 1
else:
return n * factorial(n - 1)