Javatpoint Logo
Javatpoint Logo

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

MultiplicationExample1.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 Loop

MultiplicationExample3.java

Output:

Enter the multiplicand: 17
Enter the multiplicator: 8
The product of 17 and 8 is:  136

Using Recursion

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.

Multiply Two Numbers Without Using Arithmetic Operator in Java

The above table represents that:

  • The multiplication of two negative numbers gives a positive integer.
  • The multiplication of two positive numbers also gives a positive integer.
  • The multiplication of a positive and a negative number gives a negative integer.
  • The multiplication of a negative and a positive number gives a negative integer.

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






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA