Recursion is driving me crazy!!!!!!!!!!!!! - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

# Recursion is driving me crazy!!!!!!!!!!!!!

Posts: 5Member
I am trying to learn recursion right now and I am having a very difficult time...

I am trying to multiply 2 integers recursively.

This code works as long as int y doesn't exceed 6202, otherwise I am getting a stack overflow error.

public static int multiply(int x, int y){
int result = 0;
if(x == 0 || y == 0){
result = 0;
}
else if(x == 1){
result = y;
}
else if(y == 1){
result = x;
}
else{
result = x + multiply(x, y - 1);
}
return result;
}

I am trying to write a method that uses the modular (%) function to break down x and y into their individual numbers i.e.
startMod = 10;
567
X863
----
3 * (7 * (startMod / 10) +
3 * (6 * (startMod) +
3 * (5 * (startMod) +
6 * (startMod) ( 7 * (startMod / 10)) +
6 * (startMod) ( 6 * (startMod)) +
6 * (startMod) ( 5 * (startMod)) +
8 * (startMod * 10) ( 7 * (startMod / 10)) +
8 * (startMod * 10) ( 6 * (startMod)) +
8 * (startMod * 10) ( 5 * (startMod)) == 567 * 863

This would be the recursion part of it I assume but I have worked on this for about 12 hours and can't seem to code anything that works.

It is essentially elementary multiplication.
54
X27
---
7 * 4 + 7 * 50 + 20 * 4 + 20 * 50 == 54 * 27

Any amount of help with this problem would be greatly appreciated.

Thank you
Joker2u