- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecursion.py
More file actions
Latest commit
36 lines (29 loc) · 1000 Bytes
/
Copy pathRecursion.py
File metadata and controls
36 lines (29 loc) · 1000 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
# Recursion
#find factorial of "n" number
#n=1*2*3*4*5=120 is factorial of 5
fact=5
factorial=1
foriinrange(fact):
factorial=factorial*(i+1)
print(factorial) #120 you will get in temrminal
#Now understand with function:
n=int(input('Enter number \n'))
defFactorial(n):
start=1
foriinrange(n):
start=start*(i+1)
returnstart
print(f"Factorial of {n} in {Factorial(n)} ")
print(Factorial(n))
# Here write Recursive manner to do this with base condition:
defRecursive_Factorial(n):
ifn==1orn==0: #if n value is 1 or 0 then it will simple return 1 and stop working.
return1
returnn*Recursive_Factorial(n-1)
print(f"Factorial of {n} in {Recursive_Factorial(n)} ")
#sum of natural numbers
defRecursive_Factorial(n):
ifn==1orn==0: #if n value is 1 or 0 then it will simple return 1 and stop working.
return1
returnn+Recursive_Factorial(n-1)
print(f"sum of {n} in {Recursive_Factorial(n)} ")