-
Notifications
You must be signed in to change notification settings - Fork 623
Expand file tree
/
Copy pathMultiplication (Recursive).py
More file actions
46 lines (38 loc) · 957 Bytes
/
Multiplication (Recursive).py
File metadata and controls
46 lines (38 loc) · 957 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
44
45
46
""""
----------------------------------- Multiplication (Recursive) -------------------------------------------
Given two integers M & N, calculate and return their multiplication using recursion. You can only use subtraction
and addition for your calculation. No other operators are allowed.
#### Input format :
Line 1 : Integer M
Line 2 : Integer N
#### Output format :
M x N
#### Constraints :
0 <= M <= 1000
0 <= N <= 1000
#### Sample Input 1 :
3
5
#### Sample Output 1 :
15
#### Sample Input 2 :
4
0
#### Sample Output 2 :
0
"""
def multiplyNumbers(m, n):
if m==0 or n==0:
return 0
if n>0:
smallAns = multiplyNumbers(m,n-1)
return smallAns + m
else:
smallAns = multiplyNumbers(m,n+1)
return smallAns - m
# Main
from sys import setrecursionlimit
setrecursionlimit(11000)
m=int(input())
n=int(input())
print(multiplyNumbers(m,n))