tags:

views:

978

answers:

10

Can any body tell how to write a multiplication function (in C) using recursion?

+2  A: 

Add it to itself over and over again, N times. That is if you want to multiply a number by N..

Amit
+3  A: 

Here is the function:

int mulitplication(x,y)
{
    if (y==0)
    {
      return 0;
    }
    else
      return x+multiplication(x,y-1);
    }
}
nandokakimoto
Heh... you win by 46 seconds! :)
Randolpho
StackOverflow, here we come! XD
Vilx-
@Vilx: Maddy *did* say recursion. And it will only stack overflow for very large multipliers (values of y)
Randolpho
@vilx: Of course, but where else would you go to get such a perspicacious answer.
Jonathan Leffler
Anybody want to fix the problem with negative values of y?
Jonathan Leffler
+2  A: 

You'll have to be way more specific if you want good help. But here's a recursive function in C that multiplies two positive integers:

int multiply(int multiplicand, int multiplier)
{
  if(multiplier == 0)
  {
    return 0;
  }
  return multiply(multiplicand, multiplier - 1) + multiplicand;
}

Jonathan Leffler wrote: And if multiplier is negative?

Ok:

int multiply(int multiplicand, int multiplier)
{
  if(multiplier == 0)
  {
    return 0;
  }
  if(multiplier < 0)
  {
    return multiply(multiplicand, multiplier + 1) - multiplicand; //Edit: changed "+ multiplicand" to "- multplicand"
  }
  return multiply(multiplicand, multiplier - 1) + multiplicand;
}

Mark Byers wrote: The negative version is still wrong.

Grumble, grumble. Serves me right for writing from memory and without testing. This one is tested for many integer ranges, negative and positive, odd and even. Should work for any integer value. Merry Wintereenmas.

Randolpho
And if multiplier is negative?
Jonathan Leffler
The negative version is still wrong.
Mark Byers
+2  A: 

if i am not wrong , is it like this...

int mul(int a, int b)
{
if(b==1)
return a;
else
return a+mul(a,b-1);
}
Madhan
hm what about b <= 0 ;-)
Gamecat
+5  A: 

OK, let's be original. :)

unsigned int mul(unsigned int a, unsigned int b)
{
    if ( !a || !b ) return 0;
    if ( a == 1 ) return b;
    if ( b == 1 ) return a;

    return mul(a-1, b-1)+a+b-1;
}
Vilx-
Ooooh, I see what you did there.
Randolpho
wat happens if both numbers are negative?-3*-3 =9;but its not working if both are negative numbers...
Madhan
Hello? **Unsigned** int?
Vilx-
wat could be the solution if both numbers are to be negative???
Madhan
See my other answer.
Vilx-
+2  A: 

Alternative version:

int mulInner(int a, int b, int c)
{
    if (b == 0)
        return a*c;
    return mulInner(a, b-1, c+1);
}
int mul(int a, int b)
{
    return multInner(a, b, 0);
}

Hey, he didn't say not to use the operator*...

shoosh
Extra credit: doesn't work with negative numbers!
shoosh
+5  A: 

If you really want to impress your peers and your teacher, submit this - it's both recursive and fast!

int mul(int a, int b)
{
    if ( a < 0 ) return -mul(-a,b);
    if ( b < 0 ) return -mul(a,-b);
    if ( b == 0 ) return 0;
    return (mul(a,b>>1)<<1)+(b&1?a:0);
}

Added: As an added bonus, this correctly handles both positive, negative and 0 values.

Vilx-
excellent answer, sir can u explain the logic behind the psuedo code?
Madhan
Nope. XD Whenever I give an answer to a homework, I either give it in pseudocode, give it incomplete, or give it so twisted and "clever" that the person who asks for it has no chance of explaining how it works (if he/she decides to give it as his/her own). :)
Vilx-
Hmm... doesn't look much like pseudocode to me. It looks like a rather correct answer. Figure out how it works, then explain it to your teacher.
D.Shawley
I love this one. I especially love what you did in the return expression. Doubling a halved multiplication? And the non-operation... genius!
Randolpho
@Randolpho - this effectively limits the recursion. It is not a no-op even though it does not affect the result of the function, it affects the runtime characteristics.
D.Shawley
Why do you have two answers?
Roger Pate
@Roger - Because they **are** two answers? O_o
Vilx-
+2  A: 

This only works if the first operand is larger than zero, but at least it's short and confusing. ;)

int mul(int a, int b) {
  return b + (--a ? mul(a, b) : 0);
}

However, I'm not sure that the evaluation order is defined, so you might have to move the decrement outside the expression:

int mul(int a, int b) {
  a--;
  return b + (a ? mul(a, b) : 0);
}
Guffa
+1  A: 

int mul(int a,int b) { if(b==1) return a; else return(a+(mul(a,b-1))); }

rashmi rani panda
+5  A: 

50 characters:

m(x,y){return!y?0:rand()%2?m(x,y+1)-x:m(x,y-1)+x;}

I do hope I score points for use of the rand() function (especially in that way), and general syntax niceties. Depending on your system library and the phase of the moon, be warned that recursion can lead to segfault for high values of the arguments, such as calculating 2 × 3.

FX