Using Python, we can calculate factorials using loops. Defining a iterative function to find the factorial of a nonnegative integer in Python can be done in the following code.
Below is a function which uses a for loop to find the factorial of a number.
def factorial_with_for_loop(n):
if isinstance(n,int) and n >= 0:
if n == 0:
return 1
else:
factorial = 1
for x in range(1, n + 1):
factorial = factorial * x
return factorial
else:
return "Not valid input"
We can also use a while loop to find factorials.
def factorial_with_while_loop(n):
if isinstance(n,int) and n >= 0:
if n == 0:
return 1
else:
factorial = 1
while n > 1:
factorial = factorial * n
n = n - 1
return factorial
else:
return "Not valid input"
When working with numbers, one common calculation which is easy to perform in Python is finding the factorial of a number.
We can find the factorial of a number in Python in a number of ways.
One such way is to use iteration and loops to calculate the factorial of a number.
To calculate factorials using loops, we just need to loop from 1 to the integer and keep track of the cumulative product of all of the numbers in between – including the integer.
Using For Loop to Calculate Factorial of Number in Python
We can use for loops to find the factorial of a number in Python.
To define a function using a for loop to find the factorial of a number in Python, we just need loop from 1 to n, and update the cumulative product of the index of the loop.
Below is a iterative function for calculating the factorial of a number using a for loop. I’ve also included some input validation to make sure that the input is a nonnegative integer.
def factorial_with_for_loop(n):
if isinstance(n,int) and n >= 0:
if n == 0:
return 1
else:
factorial = 1
for x in range(1, n + 1):
factorial = factorial * x
return factorial
else:
return "Not valid input"
print(factorial_with_for_loop(3))
print(factorial_with_for_loop(5))
print(factorial_with_for_loop(8))
print(factorial_with_for_loop(12))
#Output:
6
120
40320
479001600
Using While Loop to Calculate Factorial of Number in Python
We can also use while loops to find the factorial of a number in Python.
To define a function using a for loop to find the factorial of a number in Python, we just need loop from the number n to 1, subtracting 1 from n each time, and update the cumulative product of the numbers in each step of the loop.
Below is a iterative function for calculating the factorial of a number using a for loop. I’ve also included some input validation to make sure that the input is a nonnegative integer.
def factorial_with_while_loop(n):
if isinstance(n,int) and n >= 0:
if n == 0:
return 1
else:
factorial = 1
while n > 1:
factorial = factorial * n
n = n - 1
return factorial
else:
return "Not valid input"
print(factorial_with_while_loop(3))
print(factorial_with_while_loop(5))
print(factorial_with_while_loop(8))
print(factorial_with_while_loop(12))
#Output:
6
120
40320
479001600
Hopefully this article has been useful for you to define a factorial program in Python using for loops and while loops to calculate factorials.