python

Factorial in Python

Factorial of a number is the product of all integers between 1 and itself. To find a factorial of a given number, we use a for loop over a range from 1 to X(number entered). Remember that range() function excludes the stop value. Therefore, the stop value must be the input number + 1.

Each number in the range is multiplied cumulatively into the ‘fact’ variable which is initialized to 1.
 

 

Factorial in Python
nbr = int(input('Enter a number : '))
fact = 1
for i in range(1, nbr+1):
  fact = fact * i
print (nbr,'! = ',fact)

Output:

Enter a number : 3
3 ! =  6
mcqMCQPractice competitive and technical Multiple Choice Questions and Answers (MCQs) with simple and logical explanations to prepare for tests and interviews.Read More

Leave a Reply

Your email address will not be published. Required fields are marked *