Python Program to Find Factorial of any Number
Factorial is the product of all positive integers from 1 to the given number. Factorial of a number n is denoted by n!, and it is defined as:
n! = n * (n-1) * (n-2) * ... * 2 * 1
Approach
The factorial of a number can be calculated using a for loop. We can iterate from 1 to the given number and calculate the product of all the numbers.
Algorithm
- Take input from the user and store it in a variable.
- Initialize a variable to store the factorial value as 1.
- Use a for loop to iterate from 1 to the given number.
- Multiply the factorial value with the current number in each iteration.
- Print the factorial value.
Code
```python
# Python program to find factorial of a number
# take input from the user
num = int(input("Enter a number: "))
# initialize the factorial value as 1
factorial = 1
# iterate from 1 to the given number
for i in range(1, num+1):
# multiply the factorial value with the current number
factorial *= i
# print the factorial value
print("Factorial of", num, "is", factorial)
```
Output
```
Enter a number: 5
Factorial of 5 is 120
```
Thus, the factorial of the given number is calculated using a for loop in Python.