Factorial of a number

To find the factorial of a given number by defining a function

A factorial is the product of all the integers from 1 to that number.

n!=n*(n-1)…..*1

In this code, we use the concept of a recursive function, that is, the functions are called multiple times while executing it once. 

Note: Loved the post? You too can publish your article on “Python for fun” which will be loved by millions. Contribute an article!

							
							
					def factorial(n):
    if n <= 0:
        return(1)
    else:
        val=n*factorial(n-1)#Since n!=n*(n-1)!
        return(val)
m=input("Enter a number")#taking input from user
num=int(m)#By default, the input is considered to be a string. so we convert it into an integer
print(factorial(num))				
			
0 0 votes
Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments