Computer >> Computer tutorials >  >> Programming >> Python

How to Find the Factorial of a Number using Python?


Factorial of a number is the product of all integers between 1 and itself. To find factorial of a given number, let us form a for loop over a range from 1 to itself. Remember that range() function excludes the stop value. Hence stop value should be one more than the input number. 

Each number in range is cumulatively multiplied in a variable f which is initialised to 1

Example

num=int(input('enter a number'))
f=1
for i in range(1,num+1):
  f=f*i
print ('factorial of', num, '=',f)

Output

Sample run of above code −

enter a number5
factorial of 5 = 120