• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Basics / Find Factorial Of A Number In Python

Find Factorial Of A Number In Python

Author: Aditya Raj
Last Updated: January 3, 2022

In mathematics, we can perform different operations on any given number. One such operation is finding the factorial of a number. In this article, we will discuss what a factorial is and how we can find the factorial of a number in python.

What Is Factorial Of A Number?

The factorial of a number N is defined as the product of all the numbers from 1 to N. In other words, to find the factorial of a given number N, we just have to multiply all the numbers from 1 to N.

For instance, the factorial of 5 will be calculated as 1x2x3x4x5 i.e 120. Similarly, the factorial of 6 will be 1x2x3x4x5x6 i.e. 720.

The factorial of 0 has been defined to be 1. On the other hand, factorial is not defined for negative integers. 

Having known this much, let us devise an algorithm to find the factorial of a given number.

Find Factorial Of A Number In Python

To find the factorial of any given number N, we just have to multiply all the numbers from 1 to N. Before doing this, we will first check if the given number is a negative number. If yes, we will inform the user that factorial is not defined for the given number. If the input number is 0, we will say that factorial is 1.

In the case of positive integers, we can define a variable “myFact” and find the product of all the numbers from 1 to N using the for loop in python as follows.

def factorial(N):
    if N < 0:
        return -1
    if N == 0:
        return 1
    myFact = 1
    for i in range(1, N + 1):
        myFact = myFact * i
    return myFact


input_number = 0
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = 6
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = -10
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))

Output:

Factorial of 0 is 1.
Factorial of 6 is 720.
Factorial not defined for negative number -10.

Alternatively, we can use a while loop to find the factorial. Here, we will use a count variable that starts from 1, goes till N, and is incremented by 1 during each iteration of the while loop. In each iteration, we will multiply the count to the product of previous numbers. Thus, we can calculate the factorial of a number using a while loop in python as follows.

def factorial(N):
    if N < 0:
        return -1
    if N == 0:
        return 1
    myFact = 1
    count = 1
    while count <= N:
        myFact = myFact * count
        count = count + 1
    return myFact


input_number = 0
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = 6
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))
input_number = -10
output = factorial(input_number)
if output == -1:
    print("Factorial not defined for negative number {}.".format(input_number))
else:
    print("Factorial of {} is {}.".format(input_number, output))

Output:

Factorial of 0 is 1.
Factorial of 6 is 720.
Factorial not defined for negative number -10.

Conclusion

In this article, we have discussed the factorial of a number. We have also implemented programs to find the factorial of a number using the for loop and the while loop in python.  To learn more about numbers in python, you can read this article on decimal numbers in python. You might also like this article on complex numbers in python.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics Author: Aditya Raj

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us