Python Program to Find Factorial of Number Using Recursion

Created with Sketch.

Python Program to Find Factorial of Number Using Recursion

Factorial: Factorial of a number specifies a product of all integers from 1 to that number. It is defined by the symbol explanation mark (!).

For example: The factorial of 5 is denoted as 5! = 1*2*3*4*5 = 120.

See this example:

  1. def recur_factorial(n):
  2.    if n == 1:
  3.        return n
  4.    else:
  5.        return n*recur_factorial(n-1)
  6. # take input from the user
  7. num = int(input(“Enter a number: “))
  8. # check is the number is negative
  9. if num < 0:
  10.    print(“Sorry, factorial does not exist for negative numbers”)
  11. elif num == 0:
  12.    print(“The factorial of 0 is 1”)
  13. else:
  14.    print(“The factorial of”,num,“is”,recur_factorial(num))

Output:

Python Function Programs9

 

Leave a Reply

Your email address will not be published. Required fields are marked *