Table of Contents
Multiply two numbers in Java using scanner| 5 different ways
In this article, we will discuss the concept of Java Code Examples – Multiply two numbers in Java using scanner| 5 different ways
In this post, we are going to learn how to write a program using s different Ways to multiplication of Two Numbers (With Examples) in the Java programming language.

Different Ways to multiplication
here, I’ll give you separate short programs for each method so it’s easier to understand
Program 1
Multiply two numbers in Java using scanner
Multiplication of two numbers using multiplication (*) operator with user input. The simplest and most common way to Multiplication of two numbers
// Multiplication of two numbers in Java
// Basic multiplication using * operator
import java.util.Scanner;
class Basic_Multiplication{
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input first number: ");
int num_1=sc.nextInt();
System.out.print("Input second number: ");
int num_2=sc.nextInt();
System.out.println("Multiplication of two numbers: "+
(num_1*num_2));
}
}
When the above code is executed, it produces the following result
Input first number: 7 Input second number: 9 Multiplication of two numbers: 63
Multiply two numbers in Java using Method
Multiplication of two numbers using user defined method with user input.
Program 2
//Multiplication of two numbers
//in Java programming using a method
import java.util.Scanner;
class MulTwoNum{
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
System.out.println("Input value for num1:");
int num1=input.nextInt();
System.out.println("Input value for num2:");
int num2=input.nextInt();
int mul=mulNum(num1,num2);
System.out.println("The Multiplication is: "+mul);
}
public static int mulNum(int a, int b){
return a*b;
}
}
When the above code is executed, it produces the following result
Input value for num1: 6 Input value for num2: 9 The Multiplication is: 54
Multiply two numbers in using constructor
Multiplication of two numbers using constructor with user input.
Program 3
// Multiplication of two numbers in Java
// Multiplication using constructor with user input
import java.util.Scanner;
class Mul_Cons {
private int result;
//Constructor that takes two nubers and multioly them
public Mul_Cons(int a, int b){
this.result=a*b;
}
//method for get the result
public int getResult(){
return result;
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input first number: ");
int num_1=sc.nextInt();
System.out.print("Input second number: ");
int num_2=sc.nextInt();
//create an object for class
Mul_Cons mul=new Mul_Cons(num_1, num_2);
//getting the result and print it
System.out.println("product of two numbers: "+(mul.getResult()));
}
}
When the above code is executed, it produces the following result
Input first number: 12 Input second number: 11 product of two numbers: 132
Multiply two numbers in using recursion
Multiplication of two numbers using recursion with user input.
Program 4
// Multiplication of two numbers in Java
// Multiplication using recursion with user input
import java.util.Scanner;
class Mul_Recursion {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input first number: ");
int num_1=sc.nextInt();
System.out.print("Input second number: ");
int num_2=sc.nextInt();
int product=mul(num_1,num_2);
System.out.println("product: "+product);
}
public static int mul(int a, int b){
//recursive method
if(b==0){
return 0;
}else{
return a+mul(a,b-1);}
}
}
When the above code is executed, it produces the following result
Input first number: 10 Input second number: 9 product: 90
Multiply two numbers in using for loop
Multiplication of two numbers using for loop with user input.
Program 5
// Multiplication of two numbers in Java
// Simple Multiplication using for loop
import java.util.Scanner;
class MultiplyUsingFor {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.print("Input first number: ");
int num_1=sc.nextInt();
System.out.print("Input second number: ");
int num_2=sc.nextInt();
int result=0;
for(int i=0; i<num_2; i++){
result+=num_1;
}
System.out.println("Product: "+result);
}
}
When the above code is executed, it produces the following result
Input first number: 10 Input second number: 5 Product: 50
Similar post
C++ program to multiplication of two numbers using 6 ways
C program to multiplication of two numbers using 6 ways
JavaScript program to multiplication of two numbers using 4 ways
PHP multiplication of two numbers
PHP multiplication of two numbers using function