Factorial of the number 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

program -

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

#first way
ans=1
for i in range(1,n+1):
ans=ans*i
print("Factorial is - "+str(ans))

#second way
print("Factorial is - "+str(math.factorial(n)))


output -

Enter number
5
Factorial is - 120
Factorial is - 120

-In this program, the first way use the loop which start with the one and end on the given number and continuously increase and multiple in 'ans' variable and ans have factorial of given number while in second way we use direct build-in function 'factorial()' which is define in the math library so that's by we import the math library by import math

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!