Hello everyone, In this tutorial, you will learn how to write a simple factorial program in python and the same has been tested and shared in the same post.

Factorial Program in Python

We have created a simple function called “factorial()”, it takes “number” as an argument and it will return the factorial result for the given number.

def factorial(number):
sum = 1
for i in range(1, number + 1):
sum = sum * i;
return sum

Calling the function

here we have called the “factorial” function inside the for-loop, which will invoke it 10 times by passing the number and returned output formatted and printed.

for i in range(1 , 11):
print("%d! = %d" %(i, factorial(i)))

Output

1! = 1
2! = 2
3! = 6
4! = 24
5! = 120
6! = 720
7! = 5040
8! = 40320
9! = 362880
10! = 3628800

Tags:

No responses yet

Leave a Reply

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