Python program to find factorial of a number
Description: Write a Python program to find the factorial of a number using a loop.
Rules and hints:
- Factorial of 5 is 5 x 4 x 3 x 2 x 1 = 120.
- Start with 1 and multiply by each number.
Python program for factorial using a loop
num = 5
factorial = 1
for i in range(1, num + 1):
factorial = factorial * i
print("Factorial of", num, "is", factorial)
Output:
Tutorials Class - Output Window
Factorial of 5 is 120
How it works
factorialstarts with 1.- The loop multiplies it with 1, 2, 3, 4 and 5 one by one: 1, 2, 6, 24, 120.
- For 0, the loop does not run, and the answer stays 1, which is correct.
You can also solve it with a function that calls itself. See Python Functions (recursion).
Learn more about the similar topics:
| Tutorials |
|---|
| No Content Found. |
| Exercises & Assignments |
|---|
| No Content Found. |
| Interview Questions & Answers |
|---|
| No Content Found. |