Interview Question

Factorial program

Factorial multiplies integers from 1 through n, with 0! equal to 1.

💡 Concept ✅ Quick Revision 🐍 Python

Answer

The factorial of a non-negative integer n is the product from 1 through n. • By definition, 0 factorial equals 1. • An iterative solution avoids recursive call depth. • Reject negative inputs because factorial is not defined for them in this integer task.

💡 Simple Example

def factorial(n): if n < 0: raise ValueError('n must be non-negative') result = 1 for value in range(2, n + 1): result *= value return result print(factorial(5))

Output

120

⚡ Quick Revision

Factorial multiplies integers from 1 through n, with 0! equal to 1.