पाइथन मे किसी नंबर का factorials निकालने के लिए पाइथन हमे factorial () फंक्शन प्रोवाइड करता है इस फंक्शन से हमे यह फायदा होता है की हमे ज्यादा बड़ा कोड नहीं लिखना पढ़ता। factorial () फंक्शन को math module के अंदर define किया गया है।
इसे भी पढ़े - PYTHON LOOP
Factorial Program Using factorial () function
import math
a = int (input (" enter any number : "))
print("The factorial of ",a,"is :",math.factorial(a))
OUTPUT :
enter any number : 7
The factorial of 7 is : 5040
Factorial Program Using for loop
num= int(input("enter number : "))
fact = 1
if num <0:
print("Factorial value cannot be intended for negative integers")
elif num == 0:
print("factorial value for the 0 is 1.")
else :
for i in range(1,num+1):
fact = fact*i
print("factorial value for the ",num, " is = ",fact)
OUTPUT :
enter number : 5
factorial value for the 5 is = 120
Factorial Program Using while loop
num= int(input("enter number : "))
fact = 1
while(num>0):
fact = fact*num
num = num - 1
print("factorial of =",fact)
OUTPUT :
enter number : 10
factorial of = 3628800
Factorial Program Using Function
num=int(input("enter number : "))
def factorial(num):
if num <0:
print("Factorial value cannot be intended for negative integers")
elif num == 0:
print("factorial value for the 0 is 1.")
else :
fact = 1
for i in range(1,num+1):
fact = fact*i
print("factorial value for the ", fact ,"*",i , " is == ", fact)
factorial(num)
OUTPUT :
enter number : 5
factorial value for the 1 * 1 is == 1
factorial value for the 2 * 2 is == 2
factorial value for the 6 * 3 is == 6
factorial value for the 24 * 4 is == 24
factorial value for the 120 * 5 is == 120