From c1c2b3f38df1a89e667595c9053243effec4e231 Mon Sep 17 00:00:00 2001 From: agarwalpranay02 Date: Wed, 29 Oct 2025 01:00:09 +0530 Subject: [PATCH] Create factorial_iterative.py --- maths/factorial_iterative.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 maths/factorial_iterative.py diff --git a/maths/factorial_iterative.py b/maths/factorial_iterative.py new file mode 100644 index 000000000000..429dce979a74 --- /dev/null +++ b/maths/factorial_iterative.py @@ -0,0 +1,16 @@ +def factorial_iterative(n: int) -> int: + """ + Return the factorial of n using an iterative approach. + Raises ValueError for negative inputs. + """ + if n < 0: + raise ValueError("Input must be a non-negative integer") + result = 1 + for i in range(1, n + 1): + result *= i + return result + + +if __name__ == "__main__": + # simple demonstration + print(factorial_iterative(5)) # expected 120