'How to find out factorial number through user input in python?
Solution 1:[1]
Please Remove Line 2 on code:
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input("enter the number"))#python 3.x
print(factorial(n))
Solution 2:[2]
You can use math library in python.
import math
def factorial():
n= int(input("enter a number"))
print("Factorial of number ", math.factorial(n))
factorial()
Or
def factorial():
n = int(input("enter a number:"))
factorial = 1
if n == 0:
print("The factorial of 0 is 1")
else:
for i in range(1,n + 1):
factorial = factorial*i
print("The factorial of number:",factorial)
You can also add a check for number below 0 and use elif .
Or,
def factorial(n):
if n == 0:
return 1
else:
return n * factorial(n-1)
n = int(input("enter the number"))
print(factorial(n))
Inputs by default is String, You need to convert it to int
Solution 3:[3]
By for:
num=int(input("Enter The Number to show it factorial:"))
fact=1
for x in range(1,num+1):
fact*=x
print("the factorial of this number is({})".format(fact))
By while:
n=int(input("Enter The Number:"))
x=1
fact=1
while(x<=n):
fact*=x
x+=1
print(fact)
Solution 4:[4]
import math
print(math.factorial(int(input("enter the number"))))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 | |
| Solution 3 | SaLeH |
| Solution 4 | 0xC0000022L |

