Multiply two numbers without using arithmetic operators in java

Let’s learn multiply two numbers without using arithmetic operators in java.

Multiply two numbers without using arithmetic operators in java

Let’s learn how to multiply two numbers without using arithmetic operators using for loop.

import java.util.Scanner;
public class MultiplyWithoutArithmeticOperators
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      int number1, number2, multiply = 0;  
      System.out.print("Please enter first number: ");  
      number1 = sc.nextInt();
      System.out.print("Please enter second number: ");  
      number2 = sc.nextInt();  
      // loops until condition becomes false  
      for(int a = 1; a <= number1; a++)  
      {
         multiply = multiply + number2;  
      }  
      System.out.println("Multiplication of " + number1 + " and " + number2 + " is: " + multiply);
      sc.close();
   }
}

Output:

Please enter first number: 7
Please enter second number: 7
Multiplication of 7 and 7 is: 49


Let’s see another logic to multiply two numbers without using arithmetic operators where we are using static method.

import java.util.Scanner;
public class MultiplyWithoutArithmeticOperators
{
   public static void main(String[] args)
   {
      int multiply = 0;
      Scanner sc = new Scanner(System.in);   
      System.out.print("Please enter first number: ");  
      int number1 = sc.nextInt();  
      System.out.print("Please enter second number: ");  
      int number2 = sc.nextInt();  
      for(int a = 0; a < number2; a++)
      {
         multiply = multiplication(multiply, number1);  
      }  
      System.out.print("Multiplication of " + number1 + " and " + number2 + " is: " + multiply);
      sc.close();
   }
   static int multiplication(int number1, int number2)  
   {
      for(int a = 0; a < number2; a++)  
         number1++;  
      return number1;  
   }
}

Output:

Please enter first number: 12
Please enter second number: 5
Multiplication of 12 and 5 is: 60


Now let’s learn to multiply two numbers without using arithmetic operators using while loop.

import java.util.Scanner;
public class MultiplyWithoutArithmeticOperators
{
   public static void main(String[] args)
   {
      int multiply = 0;
      Scanner sc = new Scanner(System.in);   
      System.out.print("Please enter first number: ");
      int number1 = sc.nextInt();
      System.out.print("Please enter second number: ");
      int number2 = sc.nextInt();  
      for(int a = 0; a < number2; a++)  
      {    
         multiply = multiplication(multiply, number1);  
      }  
      System.out.print("Multiplication of " + number1 + " and " + number2 + " is:  " + multiply);
      sc.close();
   }
   static int multiplication(int number1, int number2)  
   {
      // loops if number2 is greater than 0      
      while(number2 != 0)  
      {
         // using bitwise AND operator      
         int x = (number1 & number2);  
         number1 = number1 ^ number2;  
         number2 = x << 1;    
      }  
      return number1;  
   }
}

Output:

Please enter first number: 17
Please enter second number: 7
Multiplication of 17 and 7 is: 119


Finally let’s learn how multiply two numbers without using arithmetic operators using recursion.

public class MultiplyWithoutArithmeticOperators
{
   public static void main(String[] args)
   {
      System.out.println(multiplication(-7, 10)); 
      System.out.println(multiplication(-17, -27));
   }
   static int multiplication(int number1, int number2)   
   {
      // checking number2 is 0 or not  
      if(number2 == 0)  
         // returns 0 if any number multiplied by 0  
         return 0;  
      // checks if number2 is greater than 0 or not  
      if(number2 > 0)  
         // calculating multiplication and return  
         return (number1 + multiplication(number1, number2 - 1));  
      // executes if number2 is negative  
      if(number2 < 0)  
         return -multiplication(number1, -number2);
      return -1;
   }
}

Output:

-70
459


Also read – polymorphism in java