Factorial program in python using function : we have provided Factorial program using math. factorial , using the recursion and without using the recursion function. The factorial is the non negative integer that product of all positive integers which is less than or equal to the number for the factorial. The factorial is denoted by the exclamation sign as the (!). The rule of the factorial number 0 is always 1. The factorial of any numbers us the product of all the integers from the 1 to that given number. The example as factorial of 6 is the 1*2*3*4*5*6=720. The factorial does not exist of the negative numbers and the factorial of 0 number is 1.
Example:-
The factorial of the 4 number is 24.
Example:-
num=int (input (“Enter a number :”))
factorial=1
if num<0:
print(“Sorry, factorial does not exist for the negative numbers”)
elif num==0:
print(“The factorial of 0 is 1”)
else
for i in range (1, num+1):
factorial=factorial*i
print(“The factorial of”, num,”is”, factorial)
Output:-
Sorry, factorial does not exist for the negative numbers
The factorial can be removed by changing the value of number. And the number is stored in num.
Then it is passed to the recur_factorial function to compute the factorial of number.
def recur_factorial (n):
if n ==1:
return n
else:
return n*recur_factorial (n-1)
num=7
if num<0:
print(“Sorry, factorial does not exist for negative numbers”)
elif num==0:
print(“The factorial of 0 is 1”)
else:
print (“The factorial of”, num,”is”, recur_factorial (num))
Output:-
The factorial of 7 is 5040
Problem:-
It will take number and find the factorial of number without using the recursion function.
The source code in the python programming language to find the factorial without using the recursion function.
Program:-
n=int (input (“Enter number :”))
fact=1
while(n>0):
fact=fact*n
n=n-1
print(“Factorial of number is: “)
print(fact)
Explanation:-
Example:-
Enter number: 5
Factorial of the number is: 120
n=23
fact=1
for i in range (1, n+1)\
fact=fact*i
print(“The factorial of 23 is:” end=””)
print(fact)
Output:-
The factorial of 23 is: 2585
The method is defined in the “Math” module because it has the internal implementation.
Math.factorial(x)
Parameters:-
X:-The number has factorial to compute
Return value:-
The factorials will return a desired number
The error is occurred or raised if number is negative or non integral.
Example:-
import math
print(“The factorial of 23 is:” end=””)
print(math.factorial (23))
Output:-
The factorial of 23 is: 25852
a.Exception in the math. factorial:-
importmath
print(“The factorial of -5 is:” end=””)
print(math.factorial (-5))
Output:-
The factorial of -5 is:
Runtime error:
b.Non-intergral value:-
import math
print(“The factorial of 5.6 is:” end=””)
print(math.factorial (5.6))
Output:-
The factorial of 5.6 is:
Runtime error: