Python Program to find the Factorial of number using Iteration
In this tutorial, we will discuss a Python program to find the factorial of a given number using iteration.
The factorial of a non-negative integer nn is the product of all positive integers less than or equal to nn.
Related: Python Program to find the Sum of Arithmetic Progression
Program code for Factorial of a number using Iteration in Python
# Factorial Using Iteration in Python def factorial(num): result = 1 for i in range(1, num + 1): result *= i return result number = int(input("Enter a number: ")) print(f"The factorial of {number} is {factorial(number)}.")
Explanation
- Function Definition: The
factorial
function takes an integernum
as input and returns the factorial of the number using iteration. - Main Program: The program prompts the user to enter a number and then calculates the factorial using the
factorial
function and prints the result.
Output
- When you run the above program, it will prompt you to enter a number. After entering the number, it will calculate the factorial using iteration and print the result.
Conclusion
- In this tutorial, we learned how to find the factorial of a given number using an iterative approach in a Python program. Understanding this concept is essential for solving various mathematical problems and competitive programming challenges.