BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Python Program to Find Factorial of Number

Last Updated: February 21, 2018 by Chaitanya Singh | Filed Under: Python Examples

This program takes an input number from user and finds the factorial of that number using a recursive function. Factorial of a number is the product of an integer and all the integers below it, for example the factorial of 4 is 4*3*2*1 = 24.

Program to find factorial

In this program we have defined a function factorial(). This function takes a number as an argument and finds the factorial of it.

# 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))

Output:
Python program to find factorial of a given number

Related Programs:

  1. Python Program to convert decimal to binary
  2. Python program to check if number is positive negative or zero
  3. Python program to check leap year
  4. Python program to check if a number is prime or not
  5. Python program to print Hello World

Top Related Articles:

  1. Python Program to Convert Celsius To Fahrenheit and Vice Versa
  2. Python Program to Check if a Number is Positive Negative or Zero
  3. Python Program to Convert Decimal to Hexadecimal
  4. Python Program to Find ASCII Value of a Character
  5. Python Program to Find Smallest Number in a List

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap