Python Program to Find the Factorial of a Number

In Mathematics, factorial is an important function, which is used to find how many ways things can be arranged or the ordered set of numbers.

let us discuss the definition of the factorial.

Factorial of a number is the product of all integers between 1 and itself.

In simple words, if you want to find a factorial of a positive integer, keep multiplying it with all the positive integers less than that number. The final result that you get is the Factorial of that number.

So if you want to find the factorial of 7, multiply 7 with all positive integers less than 7. Those numbers would be 6,5,4,3,2,1. Multiply all these numbers by 7 and the final result is the factorial of 7.

We want you to know about Factorial of Number, hence we are come up with the best video explanation you have ever watched on the internet, where you not only understand the logic to get Factorial of Number but also code to find it.

You still face difficulties in understanding the logic? That’s perfectly fine, we have something more for you. You can scroll down to learn more about getting Factorial of Number using multiple methods.

For all the practice Videos and Explanation on Python please click over here. Python Practice Series.

Video Explanation to Print Factorial of Number in Python

Code: To Print Factorial of Number in Python

There are various methods to get a Factorial of numbers in python. You can find source code and explanation of different methods over here.

But before that let us understand basic logic obtaining an Factorial of Number.

Consider a number 5.

To get the Factorial of 5 we will do.


So Factorial of 5 is 120.

Let’s start with our program here.

Method 1: Factorial Number using Recursion

Source Code and Output

def getFact(n):  
    return n if (n==1) else n * getFact(n - 1);  
num = 5  
print("Factorial of",num,"is",getFact(num)) 


Factorial of 5 is 120

Code Explanation Method 1: Factorial Number using Recursion

The above program is created to get the factorial of the defined number

So, here we start with defining function getFact which accept number as n and returns factorial. Let’s see how.

We have the getFact function which accepts n, inside the function block we have a return statement. It might look a bit complicated at first, but trust me it’s not that complicated.

Let’s understand if and else statement inside return first, so if we have checked number is 1. if yes function will return the same number as the factorial of 1 is the same number.

In the else section main calculation is written, number n is multiplied with all the positive numbers less than a defined number. Here recursively called the same function until the number reaches 1 and returns the factorial of the provided number.

Outside the function we have defined num as 5. You can instead write input statement to accept number from user and convert it into an integer.

After that print statement with calling of getFact function and passing variable num as parameter.

So the result will be Factorial of 5 is 120.

I hope now the concept of Factorial number is clear to you.

Method 2: Factorial Number using In-Built function

Source Code and Output

import math
num = 5
print("Factorial of", num, "is", math.factorial(num))


Factorial of 5 is 120

Code Explanation Method 2: Factorial Number using In Built function

This is even more simpler than previous method. No complex logic no calculation. Let’s see how.

We don’t have to do calculation, all that code is already in math library’s factorial function.

Here we are importing math library and use in-build function to calculate factorial.

After importing math library, we will define num and assign value 5

Then we will write print statement and inside print we will use factorial function from math library.

By using math.factorial and pass num as parameter. That’s it. It will return factorial of that number.

Isn’t it cool to just call a function and get the factorial of that number? This is an In-build function, where we don’t have to code much because it is predefined in python, just call it, pass the desired parameter and you will get the result.

It’s as simple as that.

Above program will give you result like Factorial of 5 is 120

Facebook Comments
(Visited 1 times, 1 visits today)

Leave A Comment

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