C ++ Recursively multiply 2 integers using addition

I am trying to write code for homework that uses recursion and addition to multiply two integers. But at present, I am getting a lot of error messages, and I'm not even sure that I am on the right track. And, just for verification, it's recursive, isn't it? I wrote another program for this problem, which worked great until I realized that it was not really recursive. Here is the full code:

#include <iostream>
#include <iomanip>
using namespace std;
int result;
int m;
int n;

int rmultiply(int m, int n)
{
    if(n > 1)
        return(m + (rmultiply(n - 1)));

    else if ((m == 0) || (n == 0))
        return 0;
    else if (n == 1)
        return m;
}

int main(m, n)
    {
    cout << "Enter two integers to multiply" << endl; //prompt user to input 2 integers
    cin >> m >> n; //store them in variable m and n

    result = rmultiply(m,n);
    cout << result;
    }

Mistake 1: Too few arguments for function 'int rmultiply (int, int)'

return(m + (rmultiply(n - 1)));

Warning: control reaches the end of a non-void function

Error 2 (for lines of code below): list of expressions processed as a compound expression in the initializer

Error 3: Expected ',' or ';' before the '{' token

:

int main(m, n)
{

!

0
3

,

return(m + (rmultiply(n - 1)));

.

, m n - . , . m n rmultiply m n ( ).

,

main(m, n)

.

main , . int main

+1
int product(int a, int b)
{
    if (b==1)
    {
        return a;
    }
    else
    {
        return a + product(a, b - 1);
    }
0
   int f(int x,int y)
  {
        if (y==0)
           return 0;
       int z=f(x,y/2);
         if (y % 2==0)
           return 2*z;
        else
           return x+2*z;
 }
0

Source: https://habr.com/ru/post/1686193/


All Articles