-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrecursion.py
More file actions
30 lines (22 loc) · 872 Bytes
/
Copy pathrecursion.py
File metadata and controls
30 lines (22 loc) · 872 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
""" Recursion:
Recursion is a function which call itself..
It's used to directly use a mathematical as function..
EXAMPLE:
factorial(n) = n x factorial (n-1)
This function can be defined as follows: """
# Print Factorial using recursion ..
n = int(input("Enter a number: "))
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n*factorial(n-1)
print(f"The Factorial of this number is: {factorial(n)}")
# Here are the workflow for factorials in recursion
""" This work as follows:
Factorial(5)
5 x Factorial(4)
5 x 4 x Factorial(3)
5 x 4 x 3 Factorial(2)
5 x 4 x 3 x 2 Factorial(1)
5 x 4 x 3 x 2 x 1 -----> 120 """