syntax to find factorial of a number
Title
Question
Respected sir/mam, in the 2 question we got to know how to find the factorial of the numbers from 1 to 20 . could you please tell me how to find the factorial of a particular number say factorial of 10 i.e 10! ?
Python-3.4.3 Getting-started-with-for 09-10 min 50-60 sec
Answers:
def factorial(n):
fact = 1
if n < 0:
return "Factorial does not exists for negative numbers"
elif num == 0:
return 1
else:
for i in range(1, n + 1):
fact = fact * i
return fact
factorial(10)
num=int(input('enter a number'))
fact=1
for i in range(1,num+1):
fact=fact*i
print ('factorial of', num, '=',fact)
but how do i write a code for negative input values?...like factorial of negative indices does not exist
In this case too you might need to use if and elif, if the num user is providing doesnt satisfy the if or elif then for loop in else condition will be executed
Login to add comment
Login to add comment