saving . . . saved syntax to find factorial of a number has been deleted. syntax to find factorial of a number has been hidden .
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 17-01-21, 8:12 p.m. shraddhakb1893@gmail.com

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)
15-03-21, 12:25 p.m. iakashchavan
Thanks a lot...
15-03-21, 12:49 p.m. shraddhakb1893@gmail.com
Traceback (most recent call last):
  File "C:\python\factorial.py", line 12, in <module>
    factorial(10)
  File "C:\python\factorial.py", line 5, in factorial
    elif num == 0:
NameError: name 'num' is not defined
it says this!

15-03-21, 7:33 p.m. shraddhakb1893@gmail.com
just write n instead of num.
11-08-21, 7:39 p.m. nehamuthreja2@gmail.com

Login to add comment


num=int(input('enter a number'))
fact=1
for i in range(1,num+1):
    fact=fact*i
print ('factorial of', num, '=',fact)

15-03-21, 7:50 p.m. shraddhakb1893@gmail.com
but how do i write a code for negative input values?...like factorial of negative indices does not exist

15-03-21, 7:52 p.m. shraddhakb1893@gmail.com
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
11-08-21, 7:41 p.m. nehamuthreja2@gmail.com

Login to add comment


Log-in to answer to this question.