« Multiplication using recursion

Problem

Multiplication using recursion

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

Solution

1#include <iostream>
2#include <iomanip>
3#include <algorithm>
4#include <string>
5#include <cstring>
6#include <vector>
7#include <cmath>
8#include <map>
9#include <climits>
10// climits for INT_MIN
11#include <unordered_map>
12using namespace std;
13
14int multiplyNumbers(int m, int n)
15{
16 if (n == 0)
17 {
18 return 0;
19 }
20 int ans = multiplyNumbers(m, n - 1);
21 ans = ans + m;
22 return ans;
23}
24
25int main()
26{
27 int m, n;
28 cin >> m >> n;
29 cout << multiplyNumbers(m, n) << endl;
30 return 0;
31}