How to multiply two numbers without using the multiple operator (*) in Java

3 Answers

0 votes
class MultiplyTwoNumbersWithoutUsingMultipleOperator_Java {
    public static int multiply(int a, int b) {
        int mul = 0;
        
        for (int i = 1; i <= a; i++) {
            mul = mul + b;
        }
        
        return mul;
    }
    
    public static void main(String[] args) {
        int a = 3, b = 9;
  
        System.out.format("%d * %d = %d\n", a, b, multiply(a, b));
    }
}

 
/*
run:
     
3 * 9 = 27
      
*/

 



answered Mar 21, 2016 by avibootz
edited Aug 31, 2024 by avibootz
0 votes
class MultiplyTwoNumbersWithoutUsingMultipleOperator_Java {
    public static int multiply(int a, int b) {
        if ((a == 0) || (b == 0)) {
            return 0;
        }
        else {
            return (a + multiply(a, b - 1));
        }
    }
    
    public static void main(String[] args) {
        int a = 3, b = 9;
  
        System.out.format("%d * %d = %d\n", a, b, multiply(a, b));
    }
}

 
/*
run:
     
3 * 9 = 27
      
*/

 



answered Aug 31, 2024 by avibootz
0 votes
class MultiplyTwoNumbersWithoutUsingMultipleOperator_Java {
    public static double multiply(int a, int b) {
        return Math.pow(10, (Math.log10(a) + Math.log10(b)));
    }
    
    public static void main(String[] args) {
        int a = 3, b = 9;
  
        System.out.format("%d * %d = %.2f\n", a, b, multiply(a, b));
    }
}

 
/*
run:
     
3 * 9 = 27.00
      
*/

 



answered Aug 31, 2024 by avibootz
...