Skip to content
DeepForgeby svx
All problems
EasyAlgorithmsal-009

Iterative Factorial

Problem

Compute n! for a non-negative integer n using an iterative loop.

By definition 0! = 1 and 1! = 1, and n! = 1 * 2 * ... * n for n >= 2. Return the exact integer value.

Hint

Start from 1 and multiply by every integer from 2 through n.

Test cases

case 1
in: [5]
expected: 120
case 2
in: [0]
expected: 1
case 3
in: [1]
expected: 1
case 4
in: [12]
expected: 479001600
Starter codePython
def factorial(n):
    # Your code here
    pass

4 test cases · run your code against them in the editor