Iterative approach:

def calculate_factorial(n):
    factorial = 1
    for i in range(1, n + 1):
        factorial *= i
    return factorial

Recursive:

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

It would be great if you could share the code that you tried.

Answer from DASH on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-program-for-factorial-of-a-number
Factorial of a Number - Python - GeeksforGeeks
Input: n = 6 Output: 720 Explanation: 6! = 6 × 5 × 4 × 3 × 2 × 1 = 720 · Let's explore different methods to find the factorial of a number in Python.
Published   November 29, 2025
🌐
Programiz
programiz.com › python-programming › examples › factorial
Python Program to Find the Factorial of a Number
To understand this example, you should have the knowledge of the following Python programming topics: ... The factorial of a number is the product of all the integers from 1 to that number. For example, the factorial of 6 is 1*2*3*4*5*6 = 720. Factorial is not defined for negative numbers, ...
Discussions

Factorials
Well. What is Factorial? Factorial 5 is 5! = 1 * 2 * 3 * 4 * 5. All you need is a starting variable that is initialized to 1, and then you need to loop over all numbers from 1 (can be optimized to start at 2) to the_factorial_you_want_to_calculate. For every number that you loop over you multiply your starting variable by that number. After you looped over all the numbers, you have the factorial value in your starting variable. More on reddit.com
🌐 r/learnpython
7
2
October 11, 2021
How to write a recursive function to calculate factorial and store as list?
Growing a list recursively is possible, but uncommon. A possible trick is using [a,*function(b)]. def Factorial(N,start=1,previous=1): if start <= N: current = previous*start return [current,*Factorial(N,start+1,current)] else: return [] As u/camel_Snake remarks, using a generator is a more Pythonic approach (or a while loop). It has clear advantages as you can't run into a recursion limit. More on reddit.com
🌐 r/learnpython
15
4
March 26, 2017
How to calculate factorial using while loop?
Figuring out a factorial with a while loop is rather trivial. Part of your problem comes from your use of addition rather than multiplication. You could use addition in another loop to calculate factorial, but easier to multiply. Start with a number and a total. Multiple that number by one and store as the total. Lower the number by one. Multiple that new number by the prior calculated total. Repeat until the number equals one. The total is the factorial of the original number. . starting_number = int(input('Number? ')) number = starting_number total = 1 while number > 1: total *= number # multiple the total by the number (5, then 4, then 3...) number -= 1 # reduce the number by one print('{}! equals {}'.format(starting_number, total)) Yields: $ python3 factorial_sorta.py Number? 5 5! equals 120 More on reddit.com
🌐 r/learnpython
9
3
March 18, 2020
Python Factorial
In your main program, n takes on the values 1 through 5. When you call factorial_function, you pass it the value of n. The for loop in this function, then, will cause i to take on the values from 2 to whatever you passed in (the plus 1 accounts for range's exclusion of the stop value). That is, when n=1, n+1=2 and the for loop will have a range of (2, 2), so it won't run at all. When n=5, n+1=6, so i will be assigned the values 2 through 5. More on reddit.com
🌐 r/learnprogramming
4
0
December 26, 2021
🌐
Vultr
docs.vultr.com › python › examples › find-the-factorial-of-a-number
Python Program to Find the Factorial of a Number | Vultr Docs
December 27, 2024 - For example, the factorial of 5 (denoted as 5!) is calculated as 5 * 4 * 3 * 2 * 1, which results in 120. Factorials are commonly used in permutations, combinations, and other statistical calculations.
🌐
Great Learning
mygreatlearning.com › blog › it/software development › python program to find the factorial of a number
Python Program to Find the Factorial of a Number
October 24, 2024 - In this course, you will learn the fundamentals of Python: from basic syntax to mastering data structures, loops, and functions. You will also explore OOP concepts and objects to build robust programs. ... Factorial of a number is denoted by n! is the product of all positive integers less than or equal to n: ... So what is 10!? Multiply 10 with all the positive integers which are less than 10. ... To find '5!' again, do the same process.
🌐
AlmaBetter
almabetter.com › bytes › articles › factorial-program-in-python
Factorial Program in Python(Factorial of a Number in Python)
October 19, 2024 - Learn to write a factorial program in Python using for loops, while loops, recursion, and functions with code examples, detailed explanations and use cases ... The factorial of a number is an essential concept in mathematics and programming. It is denoted as n!, which represents the product of all positive integers from 1 to n. For example, the factorial of 5 (written as 5!) is calculated as:
🌐
GeeksforGeeks
geeksforgeeks.org › python › factorial-in-python
factorial() in Python - GeeksforGeeks
The factorial of a number n (written as n!) is the product of all positive integers from 1 to n. ... Python provides a function math.factorial() that computes factorial without writing the entire loop manually.
Published   December 18, 2025
🌐
w3resource
w3resource.com › python-exercises › python-functions-exercise-5.php
Python Exercise: Calculate the factorial of a number - w3resource
July 12, 2025 - Factorial of 5: 120 Factorial of -12: Number can't be negative or floating point! Factorial of 1.22: Number can't be negative or floating point! Factorial of 100: 93326215443944152681699238856266700490715968264381621468592963895217599993229...
Find elsewhere
🌐
Sanfoundry
sanfoundry.com › python-program-find-factorial-number-recursion
Factorial Program in Python - Sanfoundry
October 10, 2023 - If number is negative print the message otherwise find the factorial of the number. ... Factorial of a number is the product of all positive integers from 1 to that number. It is denoted by the symbol “!”. For example, factorial of 5 is 5!
🌐
W3Schools
w3schools.com › python › ref_math_factorial.asp
Python math.factorial() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... #Import math Library import math #Return factorial of a number print(math.factorial(9)) print(math.factorial(6)) print(math.factorial(12)) Try it Yourself »
🌐
WsCube Tech
wscubetech.com › resources › python › programs › factorial
Python Program to Find Factorial of a Number (Factorial Code)
October 28, 2025 - Learn how to write a Python program to find the factorial of a number. Understand efficient factorial code to solve factorial problems quickly and easily.
🌐
Toppr
toppr.com › guides › python-guide › examples › python-examples › python-program-find-factorial-number
Python Program to Find the Factorial of a Number: Python Factorial
November 10, 2021 - To find the Python factorial of a number, the number is multiplied with all the integers that lie between 1 and the number itself. Mathematically, it is represented by “!”. Thus, for example, 5! will be 5 x 4 x 3 x 2 x 1, that is 120. Factorial ...
🌐
Javatpoint
javatpoint.com › pyhton-factorial-number
Python Program to Find the Factorial of a Number
Python Factorial Number for beginners and professionals with programs on basics, controls, loops, functions, native data types etc.
🌐
PrepBytes
prepbytes.com › home › python › factorial function in python
Factorial Function in Python
February 25, 2023 - In mathematics, the factorial of a non-negative integer n, denoted by n! is the product of all positive integers less than or equal to n. For example, the factorial of 5 (written as 5!) is 5 x 4 x 3 x 2 x 1 = 120.
🌐
Shiksha
shiksha.com › home › it & software › it & software articles › programming articles › how to find the factorial of a number using python
How to Find the Factorial of a Number Using Python - Shiksha Online
August 16, 2024 - One of the easiest methods to find the factorial of a number is to use an in-built factorial() function in the Python math library, that directly returns the factorial of any non-negative number when applied to it:
🌐
PrepBytes
prepbytes.com › home › python › python program to find factorial of a number
Python program to find factorial of a number
April 26, 2023 - (exclamation mark), used to calculate the product of all positive integers up to a given non-negative integer. For example, the factorial of 5 (denoted by 5!) is calculated as 5 x 4 x 3 x 2 x 1 = 120.
🌐
Scaler
scaler.com › home › topics › python program to find factorial of a number
Python Program to Find Factorial of a Number - Scaler Topics
July 17, 2023 - At each step, the value of the loop control variable (i.e., i) gets multiplied by the variable fact, which stores the value of the factorial of the given number. ... The loop runs from 1 to num i.e. 7, and every time ‘i’ gets multiplied to the variable ‘fact’. So, corresponding values of ‘i’ and ‘fact’ at each iteration are ... i = 1, fact = 1 i = 2, fact = 2 i = 3, fact = 6 i = 4, fact = 24 i = 5, fact = 120 i = 6, fact = 720 i = 7, fact = 5040i=1,fact=1i=2,fact=2i=3,fact=6i=4,fact=24i=5,fact=120i=6,fact=720i=7,fact=5040 · In the following programs, we will add an if-else statement to handle the cases where the value of the given number is negative.
🌐
Quora
quora.com › How-do-I-write-a-Python-program-to-find-the-factorial-of-a-given-number
How to write a Python program to find the factorial of a given number - Quora
Answer (1 of 16): The easiest method would be to use Python’s inbuilt math module: [code]import math n = 9 factorial = math.factorial(n) print(factorial) #returns 362880 [/code]
🌐
YouTube
youtube.com › example program
Python Program to Find the Factorial of a Number Tutorial - YouTube
In this Tutorial you will learn to write a Python Program to find the factorial of a number using Iterative Method ( for loop ).The factorial of a positive i...
Published   August 22, 2020
Views   42K