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 OverflowGeeksforGeeks
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, ...
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
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
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
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
Top answer 1 of 5
1
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.
2 of 5
0
A possible method using a while loop:
factorial_base = int(input('Enter a positive integer:\n')) # The number to factorialize.
factorial = 1
if factorial_base > 0: # 0! = 1, so if the base is 0, just leave the factorial value as default (1).
while factorial_base != 1:
factorial *= factorial_base
factorial_base -= 1
Essentially, this will multiply 1 by the given number to factorialize (i.e. n), then by (n - 1), then by (n - 2), and so on, until the multiplication factor reaches 1.
Hope this helps.
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
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.
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.
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