Using for Loop Using IF…else Statement Using Recursion Using math. factorial()
Factorial of a Number using for Loop
Let us take up the example of python code that takes a positive integer as input to determine the factorial of positive integers. In the following code, the loop begins with one, and then it multiplies by each number that precedes the actual number whose factorial is to be determined. The following python code illustrates the factorial function using a loop. Python code: Output: The above python program takes the input of positive numbers only, and it does not have a check of negative numbers in it. In this program, the factor is 1 when j is equal to 1. When j is 2, the factor is multiplied with 2, and it will do the action till j comes at 4 to arrive at 24.
Factorial of a Number using IF…else Statement
The following python code illustrates the factorial function using function. Let us take up the following python code that takes positive integers as input to determine the factorial of positive integers. In previous python code, the check for negative numbers was not applied, making the factorial function incomplete and prone to deliver an error message if negative numbers are put as an input. In the given code, the loop begins with one, and then it multiplies by each number that precedes the actual number whose factorial is to be determined, and the function also checks for negative numbers. Python code: Output: The above python program to find factorial of a number takes the input of positive numbers only, and it does have a check of negative numbers in it using the if and else statement of python. In this program, the factor is 1 when j equals 1. When j is 2, the factor is multiplied with 2, and it will do the action till j comes at 4 to arrive at 24.
Factorial of a Number using Recursion
The following python code illustrates the factorial function using recursion. Let us take up the following python code that takes positive integers as input to determine the factorial of positive integers. In this example, a recursive function determines the factorial number. Python code: Output: – The recursion can be explained as a concept wherein the function invoked in the python module can call itself again and again. It runs until the time the python condition present in the python module is satisfied, wherein the function invoked is passed with value. In the above python program, the function number def factorial keeps calling itself recursively until and unless the number reaches zero. Once the number reaches zero, it initializes the number as 1, ending the recursion.
Factorial of a Number using math. factorial()
The following python code illustrates the factorial function using math.factorial(), which can be used by importing the math module. This function does not accept negative integers, and it throws an error message of value error when float numbers are provided. Let us take up the following python code that takes positive integers as input to determine the factorial of positive integers. Python code: Output: –
Algorithm for the Factorial Program in Python
Let us take an example that illustrates the concept of factorial. For determination of factorial 5, follow the following steps: – Here, 5! is expressed as 120. The following diagram helps in understanding the algorithm of computing factorial, and in this case, let us take an example of factorial 4!
Application of Factorial in Python
Factorial of a number has a wide level of applications in mathematics. Here are important applications of Python:
The python helps in computation, followed with print factorial in faster and more efficient terms than other available programming languages. The python code is easily understandable and can be replicated across different platforms, and the factorial python program can be incorporated in several mathematical model-building assignments.
Summary:
Factorial of a number can be described as the product or multiplication of all positive integers equal to or less than the number for which the product or factorial is being determined. There are three ways in which the factorial of a number in python can be executed.
Factorial computation using For Loop Factorial computation using recursion. Usage of user-defined function
The factorial of a number is determined for a non-negative integer, and the results are always in positive integers.
By exception of the rule, a zero factorial is at 1.
Factorial of a number has a wide level of applications in mathematics.
Learn our next tutorial about Swap two numbers without using a third variable