Python Program to Find the Factorial of a Number

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.

Do 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 Explanations 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 explanations of different methods over here.

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

Consider a number 5.

To get the Factorial of 5 we will do.

5*4*3*2*1

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

Output:

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 a 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, the 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 an input statement to accept a number from the user and convert it into an integer.

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

So the result will be Factorial of 5 is 120.

I hope now the concept of the 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))

Output

Factorial of 5 is 120

Code Explanation Method 2: Factorial Number using In-Built function

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

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

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

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

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

By using math. factorial and pass num as parameters. That’s it. It will return the 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.

The above program will give you result like Factorial of 5 is 120