forked from codevscolor/codevscolor
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_factorial.py
More file actions
43 lines (26 loc) · 785 Bytes
/
find_factorial.py
File metadata and controls
43 lines (26 loc) · 785 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# www.codevscolor.com/python-factorial-of-a-number/
# Method 1 : using for loop
def factorialUsingForLoop(n):
fact = 1
for i in range(1,n+1):
fact=fact*i
print('Factorial of the number %d is %d'%(n,fact))
if __name__== "__main__":
factorialUsingForLoop(4)
#Method 2 : using while loop
def factorialUsingWhileLoop(n):
fact = 1
while(n>1):
fact = fact*n
n = n - 1
print('Factorial is %d'%(fact))
if __name__== "__main__":
factorialUsingWhileLoop(4)
#Method 3 : using recursion
def factorialUsingRecursion(n):
if (n == 1):
return 1
else :
return n* factorialUsingRecursion(n-1)
if __name__== "__main__":
print("factorial is ",factorialUsingRecursion(4))