Definition
The factorial of a number is the product of all positive integers from 1 to that number.
Formula
For a number n:
5! = 5 × 4 × 3 × 2 × 1 = 120
4! = 4 × 3 × 2 × 1 = 24.
Algorithm:
1.Start
2.Read the number n
3.Initialize fact = 1 and i = 1
4.Check whether i <= n
5.Multiply fact by i
6.Increment i by 1
7.Repeat steps 4 to 6 until i > n
8.Print the factorial value
9.Stop
Flowchart:
Start
|
Input n
|
fact = 1
i = 1
|
i <= n ?
/ \
Y N
| |
fact=fact*i
|
i=i+1
|
Back to i<=n
N
|
Print Factorial
|
Stop
code:
n = int(input("Enter a number: "))
fact = 1
i = 1
while i <= n:
fact = fact * i
i += 1
print("Factorial =", fact)
Input:
Enter a number: 5
Output:
Factorial = 120
Top comments (0)