Python – Program for Factorial of a Number

Python – Program for Factorial of a Number

In this post, We will be going to see different ways to write a code in Python for the factorial of a number.

Formula for finding factorial of a Number N : N * (N – 1) * (N – 2) * (N – 3) * ……* 2 * 1.

Note:

 N must be a Non-Negative Integers.

Example:

Input: Number = 6
Output: Factorial of 6 is 720. (6! = 6*5*4*3*2*1)
                                               
Input: Number = 5
Output:  Factorial of 5 is 120. (5! = 5*4*3*2*1)

Now, Let’s see the Codes:

Code 1: Using math library.
In this, we are importing the whole math library into this program then use the factorial() method.


# import math library 
# inside this program
import math

# given input
number = 5

# call factorial() method of 
# math library and then print
# the result
print("Factorial of", number, 
      ":", math.factorial(number))

Output:

Factorial of 5 : 120

Code 2: Using math library.
In this, we are importing only the factorial() method from the math library.

# import factorial() method 
# of math library inside this 
# program
from math import factorial

# given input
number = 5

# call factorial() method of 
# math library and then print
# the result
print("Factorial of", number, 
      ":", factorial(number))

Output:

Factorial of 5 : 120

Code 3: Using iterative approach.

# given input
number = 6

# factorial of 0 is 1
if number == 0 :
    factorial = 1

else:
    factorial = number

# reverse looping form number - 1
# upto 2
for value in range(number - 1, 1, -1):
    
    # multiplying number one by one 
    # in each iteration
    factorial *= value
    
# showing final result 
print("Factorial of", number, 
      ":", factorial)

Output:

Factorial of 6 : 720

Code 4: By Creating user-defined function then applying iterative approach.

# Create a user-defined function
# for finding factorial of
# a given number
def findFactorial(number):
    
    # when number is either 0 or 1 
    # then simply return it
    if (number == 0 or number == 1):
        return number
    
    factorial = number

    # reverse looping form number - 1
    # upto 2
    for value in range(number - 1, 1, -1):
        factorial *= value

    return factorial

# main code
if __name__ == "__main__" :
    
    # taking input from user and
    # convert it into integer
    number = int(input("Enter a Number: "))

    # function calling and printing
    print("Factorial of", number, 
          ":", findFactorial(number))

Output:

Enter a Number: 7
Factorial of 7 : 5040

Code 5: By Creating user-defined function then applying Recursive approach.

# Create a user-defined function
# for finding factorial of
# a given number

# recursive approach
def findFactorial(number):
    
    # when number becomes 0 or 1 
    # then simply return it
    if (number == 0 or number == 1) :
        return 1
    
    # otherwise recursively compute this
    else:
        return number * findFactorial(number - 1)
    
# main code 
if __name__ == "__main__" :
    
    # taking input from user and
    # convert it into integer
    number = int(input("Enter a Number: "))
    
    # function calling and printing
    print("Factorial of", number, 
          ":", findFactorial(number))

Output:

Enter a Number: 7
Factorial of 7 : 5040

Code 6: By Creating user-defined function then applying recursive approach along with the use of the ternary operator.

# Create a user-defined function
# for finding factorial of
# a given number

# recursive approach
def findFactorial(number):

    # use of ternary operator
    
    # if number is either 0 
    # or 1 then return 1
    # otherwise recursively
    # calling function
    return 1 if number == 0 or number == 1 else number * findFactorial(number - 1)

# main code
if __name__ == "__main__" :
    
    # taking input from user and
    # convert it into integer
    number = int(input("Enter a Number: "))
    
    # function calling and printing
    print("Factorial of", number, 
          ":", findFactorial(number))

Output:

Enter a Number: 6
Factorial of 6 : 720

23 Comments

  1. I’ve read several excellent stuff here. Certainly price bookmarking for revisiting.
    I surprise how much effort you set to make the sort of wonderful informative web site.

  2. I don’t know whether it’s just me or if everybody else encountering issues with your website.
    It appears as though some of the text within your content are running off the screen. Can somebody else please provide feedback and let
    me know if this is happening to them as well? This could be a issue with my browser because I’ve
    had this happen previously. Thanks

  3. First off I want to say superb blog! I had a quick
    question in which I’d like to ask if you do not mind. I was interested to find out how you center yourself and clear your head
    prior to writing. I’ve had trouble clearing my thoughts in getting my thoughts out there.
    I truly do enjoy writing but it just seems like the first 10 to 15 minutes tend to
    be wasted simply just trying to figure out how to begin. Any ideas or tips?
    Kudos!

  4. May I simply say what a relief to discover somebody that genuinely knows what they’re talking about
    on the net. You definitely understand how to bring a problem to light and make it important.
    More and more people ought to look at this and understand this side of your story.

    I was surprised that you are not more popular because you certainly possess
    the gift.

  5. My partner and I stumbled over here from a different web address and thought I may as well
    check things out. I like what I see so now i am following you.
    Look forward to looking into your web page again.

  6. This design is spectacular! You obviously know how to keep a reader entertained.
    Between your wit and your videos, I was almost moved to start my own blog (well, almost…HaHa!) Fantastic job.
    I really enjoyed what you had to say, and more than that, how
    you presented it. Too cool!

  7. Aw, this was a really good post. Taking the time and actual efdort to roduce a great
    article… but whawt can I say… I put things
    off a lot and never seem to get nearly anything done.

  8. Hey I am so delighted I found your website, I really found you
    by error, while I was browsing on Askjeeve for something else,
    Anyways I am here now and would just like to say cheers for a incredible post and a all round thrilling blog (I also love the theme/design),
    I don’t have time to look over it all at the moment but
    I have saved it and also included your RSS feeds, so when I have time I will be back to read much more,
    Please do keep up the excellent job.

  9. Hey! This is my first comment here so I just wanted to give a quick shout out and tell you I genuinely enjoy reading your posts.
    Can you suggest any other blogs/websites/forums
    that go over the same topics? Thanks a lot!

Leave a Reply

Your email address will not be published.

📢 Need further clarification or have any questions? Let's connect!

Connect 1:1 With Me: Schedule Call


If you have any doubts or would like to discuss anything related to this blog, feel free to reach out to me. I'm here to help! You can schedule a call by clicking on the above given link.
I'm looking forward to hearing from you and assisting you with any inquiries you may have. Your understanding and engagement are important to me!

This will close in 20 seconds