problem – Java program to multiply two numbers without using arithmetic operator
In this tutorial, we will discuss the concept of multiplying two numbers without using the arithmetic operator in Java language.
In this post, we will learn how to get the product of two numbers without arithmetic operator in Java programming language
This program is used to find the multiplication of two numbers entered by the user – using for loop without arithmetic operator
import java.util.Scanner; class ProductNumWithOutope1{ public static void main(String args[]){ int product=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int n1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int n2=scan.nextInt();//get input from the user for num2 for(int i=0; i<n2; i++){ product=add(product,n1); } System.out.print("product of"+n1+"and"+n2+" is: "+product); } static int add(int num1,int num2){ for(int i=0; i<num2; i++) num1++; return num1; } }
When the above code is executed, it produces the following results
Enter the first number: 30 Enter the second number: 25 Product of 30 and 25 is : 750
This program is used to find the multiplication of two numbers entered by the user – using while loop without arithmetic operator
import java.util.Scanner; class ProductNumWithOutope{ public static void main(String args[]){ int product=0; Scanner scan=new Scanner(System.in); //create a scanner object for input System.out.print("Enter the first number: "); int n1=scan.nextInt();//get input from the user for num1 System.out.print("Enter the second number: "); int n2=scan.nextInt();//get input from the user for num2 for(int i=0; i<n2; i++){ product=add(product,n1); } System.out.print("product of "+n1+" and "+n2+" is: "+product); } static int add(int num1,int num2){ while(num2 != 0){ int num3=(num1 & num2); num1=num1^num2; num2=num3<<1; } return num1; } }
When the above code is executed, it produces the following results
Enter the first number: 20 Enter the second number: 25 Product of 20 and 25 is : 500
Similar program
Java program to Multiply two numbers
C program to multiply two numbers without using arithmetic operator
C++ program to multiply two numbers without using arithmetic operator
Python program to multiply two numbers without using arithmetic operator
Suggested for you
Datatype and variable in Java language
PHP Star Triangle pattern program In this tutorial, we will discuss about PHP Star Triangle…
PHP Full Pyramid pattern program In this tutorial, we will discuss about PHP Full Pyramid…
5 methods to add two numbers in Java In this tutorial, we will discuss the…
Python full Pyramid star pattern program In this tutorial, we will discuss the concept of…
Write a function or method to convert C into F -Entered by the user In…
How to write a function or method to convert Celsius into Fahrenheit In this tutorial,…