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. The 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.
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 a Factorial of Numbers using multiple methods.
For all the practice Videos and Explanations on Python, please click over here. Python Practice Series.
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 the basic logic of obtaining a Factorial Number.
Consider the 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.
Program to Factorial Number using In-Built function In Python
import math num = 5 print("Factorial of", num, "is", math.factorial(num))
Factorial of 5 is 120
This is an even simpler 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 the num and assign the value of 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 a Factorial of 5 is 120
If you want to learn python programming, you can refer to this Python Online Course with Certification.