This recursive function should calculate the multiplication of two integers but is always returning +1 in the result, can anyone help me?
int multiplic(int m1, int m2){
if(m2==0){
return 1;
}
return m1 + multiplic(m1,m2-1);
}
This recursive function should calculate the multiplication of two integers but is always returning +1 in the result, can anyone help me?
int multiplic(int m1, int m2){
if(m2==0){
return 1;
}
return m1 + multiplic(m1,m2-1);
}
The problem is that at the end of the recursion you return 1
instead of returning 0
and that 1
is added to the value of the account.
Just switch to 0
:
int multiplic(int m1, int m2){
if(m2==0){
return 0; // <--
}
return m1 + multiplic(m1,m2-1);
}