Multiply Two Numbers Without Using Arithmetic Operator in Java
In this section, we will learn how to multiply two numbers without using the arithmetic operator (*) in Java.
The multiplication of two numbers can be found by the repeated addition method. It means that add the number (multiplicand) into itself up to multiplicator times. The method can be used if we want to calculate the multiplication of small numbers.
Suppose, we want to multiply 3 by 4 which gives 12 as the result. The same can be achieved by adding 3 four times i.e. (3 + 3 + 3 + 3 = 12) or by adding 4 three times i.e. (4 + 4 + 4 = 12). Both give the same result. Therefore, we can implement the logic using recursion.
Using for Loop
Enter the first number: 6 Enter the second number: 16 The multiplication of 6 and 16 is: 96
Let's see another logic for the same.
Enter the first number: 37 Enter the second number: 23 product of 37 and 23 is: 851
Using while Loop
Enter the multiplicand: 17 Enter the multiplicator: 8 The product of 17 and 8 is: 136
By using the recursion, we can multiply two integers with the given constraints. To multiply a and b, recursively add a, b time.
Integers include both positive and negative numbers. The multiplier or multiplicand may hold a positive or a negative sign before the number. No sign before a number represents a positive number. If the number holds a positive or a negative sign, they follow the rules, given in the following table.
The above table represents that:
The following program also works if the numbers are negative.
The multiplication is: -45 The multiplication is: 136 The multiplication is: 144