Computer Science, asked by lavanyauday3935, 8 months ago

How to write a program to find factorial of a number in python by taking the input from the user

Answers

Answered by haharite
15

Answer:

# This Python Program finds the factorial of a number

def factorial(num):

   """This is a recursive function that calls

  itself to find the factorial of given number"""

   if num == 1:

       return num

   else:

       return num * factorial(num - 1)

# We will find the factorial of this number

num = int(input("Enter a Number: "))

# if input number is negative then return an error message

# elif the input number is 0 then display 1 as output

# else calculate the factorial by calling the user defined function

if num < 0:

   print("Factorial cannot be found for negative numbers")

elif num == 0:

   print("Factorial of 0 is 1")

else:

   print("Factorial of", num, "is: ", factorial(num))

Explanation:

This program was the one I used hope you find it useful and mark me as brainliest

Answered by anonymouslygreat
6

Answer:

There are two ways to do this. I will be using recursion method.

def fact(n):

 if n == 0:

   return 1 # because 0! = 1

 return n * fact(n - 1)

print(fact(5)) # 120

Hope it helped you. Please mark as brainliest!

Similar questions