0
0
PythonProgramBeginner · 2 min read

Python Program to Find Factorial of a Number

You can find the factorial of a number in Python using a loop like for i in range(1, n+1): result *= i or recursion with factorial(n) = n * factorial(n-1).
📋

Examples

Input0
OutputFactorial of 0 is 1
Input5
OutputFactorial of 5 is 120
Input1
OutputFactorial of 1 is 1
🧠

How to Think About It

To find the factorial of a number, think of multiplying all whole numbers from 1 up to that number. For example, factorial of 5 means 1 × 2 × 3 × 4 × 5. We can do this by starting with 1 and multiplying by each number until we reach the input number.
📐

Algorithm

1
Get the input number from the user.
2
Initialize a variable to 1 to hold the factorial result.
3
Multiply this variable by every number from 1 up to the input number.
4
After the loop ends, return or print the factorial result.
💻

Code

python
def factorial(n):
    result = 1
    for i in range(1, n + 1):
        result *= i
    return result

num = int(input("Enter a number: "))
print(f"Factorial of {num} is {factorial(num)}")
Output
Enter a number: 5 Factorial of 5 is 120
🔍

Dry Run

Let's trace factorial of 5 through the code

1

Input

num = 5

2

Initialize result

result = 1

3

Loop i=1

result = 1 * 1 = 1

4

Loop i=2

result = 1 * 2 = 2

5

Loop i=3

result = 2 * 3 = 6

6

Loop i=4

result = 6 * 4 = 24

7

Loop i=5

result = 24 * 5 = 120

8

Return result

return 120

iresult
11
22
36
424
5120
💡

Why This Works

Step 1: Start with 1

We begin with result = 1 because multiplying by 1 does not change the value and it is the identity for multiplication.

Step 2: Multiply in loop

We multiply result by each number from 1 to n to accumulate the factorial product.

Step 3: Return final value

After the loop finishes, result holds the factorial, which we return or print.

🔄

Alternative Approaches

Recursion
python
def factorial(n):
    if n == 0 or n == 1:
        return 1
    else:
        return n * factorial(n - 1)

num = int(input("Enter a number: "))
print(f"Factorial of {num} is {factorial(num)}")
Uses function calling itself; elegant but can hit limits for very large numbers.
Using math module
python
import math
num = int(input("Enter a number: "))
print(f"Factorial of {num} is {math.factorial(num)}")
Simplest and fastest way using Python's built-in library, but less educational.

Complexity: O(n) time, O(1) space

Time Complexity

The loop runs from 1 to n, so the time grows linearly with the input number.

Space Complexity

Only a few variables are used, so space stays constant regardless of input size.

Which Approach is Fastest?

Using the math module is fastest and most efficient, loops are simple and clear, recursion is elegant but uses more call stack space.

ApproachTimeSpaceBest For
LoopO(n)O(1)Clear and simple code
RecursionO(n)O(n)Elegant code but uses more memory
math.factorialO(n)O(1)Fastest and easiest for production
💡
Use a loop for clarity and recursion for elegance when finding factorial in Python.
⚠️
Forgetting that factorial of 0 is 1, not 0.