Write a recursive algorithm to multiply two positive integers m and n using repeated addition. Specify the base case and the recursive case.
#include <iostream>
using namespace std;
int RecursiveProduct(int m, int n)
{
if (0 == n)
return 0;
return m + RecursiveProduct(m, n - 1);
}
int main()
{
int m, n;
cout << "Input m = ";
cin >> m;
cout << "Input n = ";
cin >> n;
cout << "Answer = " << RecursiveProduct(m, n) << "\n";
return 0;
}
Comments
Leave a comment