About 435,000 results
Open links in new tab
  1. Python program to find the factorial of a number using recursion

    Jan 31, 2023 · In this article, we are going to calculate the factorial of a number using recursion. Examples: Output: 120. Input: 6. Output: 720. Implementation: If fact (5) is called, it will call fact (4), fact (3), fact (2) and fact (1). So it means keeps calling itself by …

  2. recursion - How would you write a non-recursive algorithm to …

    Oct 23, 2008 · Unless you have arbitrary-length integers like in Python, I would store the precomputed values of factorial() in an array of about 20 longs, and use the argument n as the index. The rate of growth of n! is rather high, and computing 20! or 21! you'll get an overflow anyway, even on 64-bit machines.

  3. Function for factorial in Python - Stack Overflow

    Jan 6, 2022 · The easiest way is to use math.factorial (available in Python 2.6 and above): If you want/have to write it yourself, you can use an iterative approach: fact = 1. for num in range(2, n + 1): fact *= num. return fact. or a recursive approach: if n < 2: return 1. else: return n * factorial(n-1)

  4. Factorial Program without Recursion in Python - Sanfoundry

    This is a Python Program to find the factorial of a number without using recursion.

  5. 5 Effective Ways to Calculate Factorials in Python Without Recursion

    Mar 7, 2024 · From Python 3.8 onwards, there’s a built-in function called prod() that can compute the product of all elements in an iterable. This can be elegantly combined with a list comprehension for a concise one-liner factorial function.

  6. Find Factorial of a Number Without Recursion in Python

    Mar 12, 2021 · Learn how to find the factorial of a number without using recursion in Python. Step-by-step guide with code examples.

  7. Python program to find factorial without recursion

    Jul 4, 2019 · Here is a Python function that calculates the factorial of a given number using a for loop: def factorial (n): if n < 0: return None. if n == 0: return 1. result = 1. for i in range (1, n+1): result *= i. return result. This function takes a single argument, n, and returns the factorial of n.

  8. Factorial of a Number – Python | GeeksforGeeks

    Apr 8, 2025 · In Python, we can calculate the factorial of a number using various methods, such as loops, recursion, built-in functions, and other approaches. Example: Simple Python program to find the factorial of a number. Explanation: This code calculates the factorial of a number n (which is 6 in this case) using a for loop.

  9. Python Programs to Find Factorial of a Number - PYnative

    Mar 31, 2025 · Learn several ways to find the factorial of a number in Python with examples, ranging from looping techniques to more concise recursive implementations and the utilization of built-in library functions.

  10. factorial of a number using function and without using function in Python

    Recursive Factorial Method. You can also calculate factorial using recursion: def factorial (n): if n == 0: return 1 else: return n * factorial (n - 1) print (factorial (5)) 120 Handling Edge Cases. Factorial of 0 is defined as 1: print (math.factorial(0)) 1 Error Handling. For negative inputs, raise an error. More on try-except. try: print ...

  11. Some results have been removed