Factorial one line python program
In this video i have a made a demo regarding Factorial one line python program. In mathematics, the factorial of a positive integer n, denoted by n!, is the product of all positive integers less than or equal to n: n!=n*(n-1)* (n-2)* (n-3).... 3* 2* 1. For example, 5!=5* 4* 3* 2*1=120 The value of 0! is 1, according to the convention for an empty product. Factorials were used to count permutations at least as early as the 12th century, by Indian scholars. Although the factorial function has its roots in combinatorics, formulas involving factorials occur in many areas of mathematics. The factorial of a number is the product of all the integers from 1 to that number. The factorial of 6 is 1*2*3*4*5*6 = 720. In this demo i have checked the factorial for 6. For testing the python program for a different number, change the value of 'num' variable in the given python factorial program.The source code for this program is
#single line code
# factorial of given number
def factorial(n):
# single line code to find factorial
return 1 if (n==1 or n==0) else n * factorial(n - 1);
# Driver Code
num = 6;
print("Factorial of",num,"is",
factorial(num))
# Iterative code:
def factorial(n):
if n < 0:
return 0
elif n == 0 or n == 1:
return 1
else:
fact = 1
while(n > 1):
fact *= n
n -= 1
return fact
# Driver Code
num = 6;
print("Factorial of",num,"is",
factorial(num))
The code demo is found at my youtube channel
No comments:
Post a Comment