Title: Factorial of a given number
n!=n*(n-1)*(n-2)*…..3,2,1
5!=5*(5-1)*(5-2)*(5-3)*(5-4)
5!=5*4*3*2*1
5!=120
Program-1:
n=int(input("Enter n value\n"))
f=1
for i in range(n,0,-1):
f=f*i
print("The factorial of ",n," is ",f)
Output:
Enter n value
5
The factorial of 5
is 120
Program-2:
n=int(input("Enter n value\n"))
f=1
for i in range(1,n+1):
f=f*i
print("The factorial of ",n," is ",f)
Output:
Enter n value
5
The factorial of 5
is 120
Comments
Post a Comment