Multiply without multiplication, division and bitwise operators, and no loops. Recursion
Posted
by
lxx22
on Stack Overflow
See other posts from Stack Overflow
or by lxx22
Published on 2012-06-22T03:14:31Z
Indexed on
2012/06/22
3:15 UTC
Read the original article
Hit count: 116
public class MultiplyViaRecursion{
public static void main(String[] args){
System.out.println("8 * 9 == " + multiply(8, 9));
System.out.println("6 * 0 == " + multiply(6, 0));
System.out.println("0 * 6 == " + multiply(0, 6));
System.out.println("7 * -6 == " + multiply(7, -6));
}
public static int multiply(int x, int y){
int result = 0;
if(y > 0)
return result = (x + multiply(x, (y-1)));
if(y == 0)
return result;
if(y < 0)
return result = -multiply(x, -y);
return result;
}
}
My question is very simple and basic, why after each "if" the "return" still cannot pass the compilation, error shows missing return.
© Stack Overflow or respective owner