-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path7.1.recusriveFunction.py
More file actions
43 lines (34 loc) · 908 Bytes
/
7.1.recusriveFunction.py
File metadata and controls
43 lines (34 loc) · 908 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
# Function is being called iteratively is called recursive function
def add_one(num):
if(num >= 9):
return num + 1
total = num + 1
print(total)
return add_one(total)
newTotal = add_one(5)
print(newTotal)
# Factorial Numbers with loop
def factorialLoop(n):
result = 1
for x in range(1,n+1):
result *= x
return result
# Factorial Numbers with recursive method
print("Factorial".center(50,"*"))
def factorial(n):
if n == 0:
return 1
else:
print(n)
return n * factorial(n - 1)
number = 4
print(f"The factorial with Recursive of {number} is {factorial(number)}")
print(f"The factorial with loop of {number} is {factorialLoop(number)}")
# Fibonacci Method with Loop
def fibonacciLoop(n):
a,b=0,1
for i in range(n):
a, b = b, a + b
return a
for i in range(10):
print(fibonacciLoop(i))