Multiply Two Numbers Without Using Arithmetic Operator in JavaIn 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 LoopMultiplicationExample1.java Output: 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. MultiplicationExample2.java Output: Enter the first number: 37 Enter the second number: 23 product of 37 and 23 is: 851 Using while LoopMultiplicationExample3.java Output: Enter the multiplicand: 17 Enter the multiplicator: 8 The product of 17 and 8 is: 136 Using RecursionBy 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. MultiplicationExample4.java Output: The multiplication is: -45 The multiplication is: 136 The multiplication is: 144 Next TopicSum of Digits of a Number in Java |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India