Factorial of number by recursion in python by R4R Team

Factorial of any number is multiplication of all 1,2,3,4,.... upto that number.

Example -

5!=1*2*3*4*5

4!=1*2*3*4

Recursion-
Function call itself is called recursion.
stack is use to implement the recursion

program -

print("Enter number")
n=int(input())

def func(n):
if n==1:
return 1
return n*func(n-1)

ans=func(n)
print("Factorial is - "+str(ans))


output -

Enter number
6
Factorial is - 720


In this program, we use recursion to find the factorial of the number, func() is a function which call again and again with the decrease value of n and when the value of n will become 1 so we return 1 to function and we backtrack the all function calling and store the result in ans

Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!