Skip to content

Commit 0f0437e

Browse files
Merge pull request #723 from bitroot9761/master
added recursive product of 2 number in cpp
2 parents ad2a72c + d67d9c4 commit 0f0437e

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

C++/recursion/productOfTwoNo.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#include <iostream>
2+
using namespace std;
3+
int multrecur(int n, int m)
4+
{
5+
if (n > 0 && m < 0) {
6+
return multrecur(m, n);
7+
}
8+
else if (n < 0 && m < 0) {
9+
return multrecur((-1 * n), (-1 * m));
10+
}
11+
if (n > m) {
12+
return multrecur(m, n);
13+
}
14+
else if (m != 0) {
15+
return n + multrecur(n, m - 1);
16+
}
17+
else {
18+
return 0;
19+
}
20+
}
21+
int main()
22+
{
23+
cout << "10 * 5 = " << multrecur(10, 5) << endl;
24+
cout << "10 * (-5) = " << multrecur(10, -5) << endl;
25+
cout << "(-10) * 5 = " << multrecur(-10, 5) << endl;
26+
cout << "(-10) * (-5) = " << multrecur(-10, -5) << endl;
27+
return 0;
28+
}

0 commit comments

Comments
 (0)