Python Program to Find Factorial of a Number

The factorial of a number is one of the most fundamental concepts in mathematics and programming. It is widely used in permutations, combinations, probability, and algorithm design. In this tutorial, we will learn how to calculate the factorial of a number in Python using multiple approaches such as loops, recursion, and built-in functions.

What is Factorial?

The factorial of a non-negative integer n is the product of all positive integers less than or equal to n. It is denoted by n! and defined as: n! = n × (n-1) × (n-2) × ... × 1 Examples: - 5! = 5 × 4 × 3 × 2 × 1 = 120 - 4! = 4 × 3 × 2 × 1 = 24 - 0! = 1 (by definition)

Why Learn Factorial Program?

This program helps you understand loops, recursion, and mathematical computation. It is also commonly asked in coding interviews and is a building block for more advanced algorithms.

Step-by-Step Algorithm (Using Loop)

  • Start the program
  • Input a number
  • Initialize result = 1
  • Loop from 1 to n
  • Multiply result with each number
  • Display result
  • End program

Python Program Using Loop

Python
# Factorial using loop

num = int(input("Enter a number: "))
factorial = 1

if num < 0:
    print("Factorial not defined for negative numbers")
else:
    for i in range(1, num + 1):
        factorial *= i

    print("Factorial:", factorial)
Sample Output:

Enter a number: 5
Factorial: 120

Code Explanation

We initialize the factorial variable to 1 and multiply it by each number from 1 to n using a loop. This gives us the final factorial value.

Using Recursion

Python
# Factorial using recursion

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

num = int(input("Enter a number: "))

if num < 0:
    print("Factorial not defined")
else:
    print("Factorial:", factorial(num))

Understanding Recursion

Recursion is a technique where a function calls itself. In the factorial function, the problem is broken down into smaller subproblems until it reaches the base case.

Using Built-in Function

Python
import math

num = int(input("Enter a number: "))

if num < 0:
    print("Invalid input")
else:
    print("Factorial:", math.factorial(num))

Handling Large Numbers

Python
# Handling large factorials
num = int(input("Enter a number: "))

result = 1
for i in range(1, num + 1):
    result *= i

print("Factorial:", result)

Real-World Applications

  • Permutations and combinations
  • Probability calculations
  • Algorithm design
  • Mathematical modeling
  • Computer science research

Common Mistakes to Avoid

  • Not handling negative numbers
  • Incorrect loop range
  • Missing base case in recursion
  • Stack overflow in recursion
  • Ignoring edge case (0!)

Advanced Enhancements

  • Memoization for faster recursion
  • Build factorial calculator GUI
  • Create web-based math tool
  • Use big integer libraries
  • Combine with combinatorics programs

Practice Exercises

  • Calculate factorial for multiple inputs
  • Compare loop vs recursion performance
  • Find permutations using factorial
  • Build math calculator
  • Generate factorial table

Conclusion

The factorial program is a fundamental exercise that strengthens your understanding of loops, recursion, and mathematical logic. By mastering this concept, you can solve more complex problems in programming and mathematics.

Note: Note: Use iterative methods for large inputs to avoid recursion limits.