Python Program for Factorial Using Loop and Recursive Function

Python Program for Factorial Using Loop and Recursive Function

Factorial of n is the product of all positive descending integers. Factorial of n is denoted by n!.

Here, 4! is pronounced as “4 factorial”, it is also called “4 bang” or “4 shriek”.

The factorial is normally used in Combinations and Permutations (mathematics).

There are many ways to write the factorial program in Python language. Let’s see the 2 ways to write the factorial program in Python.

  • Factorial Program using loop
  • Factorial Program using recursion

Factorial Using Loop in Python

num = int(input("Enter a number: "))
fact = 1

for i in range(1,num+1):
fact = fact * i

print("Factorial of ",num, "is :",fact)

Factorial Using Recursive Function in Python

# Factorial using Recursion 
# StudyGyaan.com

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

num = 4
print("Factorial of",num,"is",fact_recur(num))
  • Home
  • Python
  • Python Program for Factorial Using Loop and Recursive Function
Filed under: Python

No comment yet, add your voice below!


Leave a Reply