TheRaiderNation
Spartan
Alright, so I need some help here, I have to create a recursion method that multiplies 2 numbers a & b, both integers. The method has to take in the parameters (int a, int b). So method names is just public int multrecur(int a, int b), just for reference. But I can't figure out for the life of me how to do it. I can work with recursions though, I made one that outputted a fibonnaci number, but I just can't seem to figure out the algorithm for this one.
Also it has the precondition: 0 <= a <= 10; 0 <= b <= 10
What I have is probably completely off, but here's what I got:
and its being called with:
So 15 should be printed, but I always get 0.
Also it has the precondition: 0 <= a <= 10; 0 <= b <= 10
What I have is probably completely off, but here's what I got:
Code:
public int multrecur(int a, int b)
{
if(a<=0 || 10<=a)
{
return 0;
}
else
{
if(b<=0 || 10<=b)
{
return 0;
}
else
{
return multrecur(a+a, b-1);
}
}
}
and its being called with:
Code:
System.out.println(fb.multrecur(3,5));