**How to multiply in java: **In the previous article, we have seen Java Program to Subtract Two Numbers

In this article we are going to see how to multiply two numbers using Java Programming language.

## Java Program to Multiply Two Numbers

Before jumping to the program directly let’s know what is Multiplication.

**Explanation:**

Multiplication is one of mathematical operation where we find the product of two or more numbers. In general to add equal groups. It is represented by the symbol `*`

.

**Example:**

15*2=30

10*12=120

2500*2=5000

Let’s see different ways to multiply two numbers.

### Method-1: Java Program to Multiply Two Numbers By Using Multiplication ‘*’ Operator

**Approach:**

- Create scanner class object.
- Take user input for two numbers.
- Find the result using ‘*‘ operator.

**Program:**

import java.util.Scanner; public class Main { public static void main(String[] args) { //Scanner class object created Scanner sc = new Scanner(System.in); System.out.println("Enter two numbers: "); //Taking two numbers input from user int a = sc.nextInt(); int b = sc.nextInt(); //Multiplying two numbers using arthmetic operator * and printing result System.out.println("The multiplication of " + a + " and " + b + " is " + (a * b)); } }

Output: Enter two numbers: 100 6 The multiplication of 100 and 6 is 600

### Method-2: Java Program to Multiply Two Numbers By Using for Loop and Addition + Operator

**Approach:**

- Create scanner class object.
- Take user input for two numbers
`a`

and`b`

. - Declare an integer variable
`mul`

and initialize it to 0. - Run a for loop from 0 till
`b`

. - Add a to
`mul`

inside the loop. - Print
`mul`

.

**Program:**

import java.util.Scanner; public class Main { public static void main(String[] args) { //Scanner class object created Scanner sc = new Scanner(System.in); System.out.println("Enter two numbers: "); //Taking input of two numbers int a = sc.nextInt(); int b = sc.nextInt(); //declaring an integer variable 'mul' to hold multiplication value //and initially assigning it value as 0 int mul = 0; //Multiplying two numbers //By using for loop and addition operator for (int i = 0; i < b; i++) { mul += a; } System.out.println("The multiplication of " + a + " and " + b + " is " + mul); } }

Output: Enter two numbers: 5 20 The multiplication of 5 and 20 is 100

### Method-3: Java Program to Multiply Two Numbers By Using R**ecursion**

**Approach:**

- Create scanner class object.
- Declare two integer variables
`a`

and`b`

and take user input for two numbers. - Use the recursive function.
- If any one of the two nums is zero return 0.
- If b is negative call the method by negating b and returning negative of the returned positive value.
- If b is positive recursively call the method by reducing by 1 until b becomes 0 and return its addition with a.

**Program:**

import java.util.Scanner; public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("Enter two numbers: "); int a = sc.nextInt(); int b = sc.nextInt(); //calling multiply_two_nums() method System.out.println("The multiplication of " + a + " and " + b + " is " + multiply_two_nums(a, b)); } //multiply_two_nums() user defined method public static int multiply_two_nums(int a, int b) { /* 0 multiplied with anything gives 0 */ if (b == 0 || a == 0) return 0; // recursively call the method until b becomes 0 if (b > 0) return (a + multiply_two_nums(a, b - 1)); if (b < 0) return -multiply_two_nums(a, -b); return -1; } }

Output: Case-1 Enter two numbers: 4 5 The multiplication of 4 and 5 is 20 Case-2 Enter two numbers: 10 -12 The multiplication of 10 and -12 is -120 Case-3 Enter two numbers: 50 0 The multiplication of 50 and 0 is 0

Practice Java programming from home without using any fancy software just by tapping on this Simple Java Programs for Beginners tutorial.

**Related Java Programs:**