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 = 6Output:Factorial of 6 is 720. (6! = 6*5*4*3*2*1)Input:Number = 5Output: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

Hello, your content is very useful. I will always visit the website!

fantastic points altogether, you just won a new

reader. What may you recommend about your publish that you simply made some days ago?

Any positive? snakeskin Crocs https://ko-Fi.com/shellythomas https://linktr.ee/donna3amaya

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.

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

I read this article completely regarding the difference of most

up-to-date and preceding technologies, it’s awesome article.

This is my first time go to see at here and i am actually

happy to read everthing at one place.

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!

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.

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.

Hi there, everything is going nicely here and ofcourse every one is sharing data,

that’s genuinely good, keep up writing.

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!

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.

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.

I was recommended this website via my cousin.

I’m no longer certain whether this post is written through him as no one else understand

such special about my difficulty. You are wonderful!

Thanks!ทีเด็ดฟุตบอล

If some one wishes expert view concerning running a blog afterward i recommend him/her to pay a quick visit this weblog, Keep up the good work.

Usually I do not learn article on blogs, however I would like to say that this

write-up very forced me to take a look at and

do it! Your writing taste has been surprised

me. Thank you, quite great article.

Good response in return of this difficulty with genuine

arguments and explaining the whole thing about that.

Awesome! Its really remarkable post, I have got much

clear idea on the topic of from this post.

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!

I do not even know how I ended up here, but I thought this post was

good. I don’t know who you are but certainly you are going to a famous blogger if you are

not already 😉 Cheers!

I could not resist commenting. Very well written!

Good blog post. I absolutely love this site.

Stick with it!

Thanks very nice blog!

I could not resist commenting. Perfectly written!

My site … سعید محمد