Find the factorial of a number in python using for loop
Here you will get python program to find factorial of number using for and while loop. Show
Factorial of a number is calculated by multiplying it with all the numbers below it starting from 1. For example factorial of 4 is 24 (1 x 2 x 3 x 4). Below program takes a number from user as an input and find its factorial. Using For Loop
Output enter a number: 5 Using While Loop
Output enter a number: 4 Video Tutorial
To use a while loop to find the factorial of a number in Python: Here is how it looks in code: Let’s test that it works: Output: There you have it! To learn more about factorials and some alternative ways to calculate the factorial, please read along. In mathematics, the factorial is the product of all positive whole numbers less than or equal to a target number. The factorial is denoted with
an exclamation mark. For example, let’s calculate the factorial of 5: In layman’s terms, the factorial answers the question “In how many ways can you form a queue of n persons?”. Recursion and Factorial in PythonIn programming, recursion refers to a function that calls itself. Usually, calculating the factorial acts as an introduction to recursion in programming courses. This is because the factorial can be expressed recursively as: n! = n x (n – 1)! In this expression, there is a factorial operator (!) on both sides. Essentially, this expression says “Factorial of n equals to n times the factorial of n – 1”. Let’s use this as a basis to formulate a recursive factorial function in Python: def factorial(n): if (n==1 or n==0): return 1 else: # n! = n x (n - 1)! return n * factorial(n - 1) In this function:
Now you can test this code to make sure it works: print(factorial(5)) Output: 120 Alternatives to Calculating the Factorial in PythonYou now know what is the factorial of a number and how it is commonly calculated using recursion. But there are some alternatives, that is:
Let’s go through each of these approaches next. 1. Built-In Factorial Function in PythonUnless you are practicing your Python skills, you should use the built-in factorial function from the math module. For example: from math import factorial print(factorial(5)) Output: 120 2. While Loop Factorial in PythonAny recursive function can be written as an iterative one. In other words, you can use a regular loop instead of recursion. Although, the feasibility to convert a recursive function into an iterative function varies based on the complexity of the function. When talking about recursive factorial, it is trivial to convert it into an iterative one using a while loop. All you need to do is:
Here is how it looks in code: def factorial(n): num = 1 while n >= 1: num = num * n n = n - 1 return num 3. For Loop Factorial in PythonYou can also convert a recursive factorial function to one that uses a for loop in Python. To do this:
Here is how it looks in code: def factorial(n): num = 1 for i in range(1, n + 1): num = num * i return num Example run: print(factorial(5)) Output: 120 ConclusionToday you learned how to calculate the factorial of a number using a while loop in Python. To recap, the factorial of a number is a whole number multiplied by all positive whole numbers less than it. Commonly, you see factorial being calculated with a recursion. But it is just as fine to calculate the factorial using an iterative approach, that is, a while or a for loop. If you do not need to implement the factorial yourself, use the built-in math.factorial function in Python. Thanks for reading. Happy coding! Further Reading50 Python Interview Questions How do you find the factorial of a for loop?Factorial Program using loop. #include int main(). int i,fact=1,number;. printf("Enter a number: ");. scanf("%d",&number);. for(i=1;i<=number;i++){. fact=fact*i;. How do you find the factorial of a function in Python?For example: The factorial of 5 is denoted as 5! = 1*2*3*4*5 = 120.. def recur_factorial(n):. if n == 1:. return n.. return n*recur_factorial(n-1). # take input from the user.. num = int(input("Enter a number: ")). # check is the number is negative.. |