Java Program to Multiply Two Numbers

Program 1:

/*
Java program to find the multiplication of two integer numbers entered by the user.
Date: Saturday, 08-02-2020
@author: Ankur Saxena
*/

// Simple program: Using Scanner class
import java.util.Scanner;

public class Multiply4{
    public static void main (String[] args){
        //creating new Scanner
        Scanner sc = new Scanner(System.in);
        System.out.println ("Please enter any two numbers:\n");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        
        //compute multiplication
        int result = num1 * num2;
        
        //printing result
        System.out.println ("The multiplication of "+num1+" and "+num2+" = "+result);
    }
}


Save this file as 'Multiply4.java'
Compile: $javac Multiply4.java
Execute: $java Multiply4
Output:

Please enter any two numbers:

12
5
The multiplication of 12 and 5 = 60


Program 2:


/*
Java program to find the multiplication of two integer numbers entered by the user using Scanner class.
Date: Saturday, 08-02-2020
@author: Ankur Saxena
*/

//class and object method
import java.util.Scanner;

class Multi1{
    //creating method
    public void m1(){
        //creating new Scanner
        Scanner sc = new Scanner(System.in);
        System.out.println ("Please enter any two numbers:\n");
        int num1 = sc.nextInt();
        int num2 = sc.nextInt();
        
        //compute multiplication
        int result = num1 * num2;
        
        //printing result
        System.out.println ("The multiplication of "+num1+" and "+num2+" = "+result);
    }
}

public class Multiply5{
    public static void main(String[] args){
        //creating new object
        Multi1 t = new Multi1();
        
        //calling method
        t.m1();
    }
}


Save this file as 'Multiply5.java'
Compile: $javac Multiply5.java
Execute: $java Multiply5
Output:

Please enter any two numbers:

15
5
The multiplication of 15 and 5 = 75

Program 3:

/*
Java program to find the multiplication of two integer numbers entered by the user using BufferedReader class.
Date: Saturday, 08-02-2020
@author: Ankur Saxena
*/

//importing packages
import java.io.BufferedReader;
import java.io.InputStreamReader; //recommanded method
import java.io.IOException; 
//import java.io.*;//importing all packages not recommanded

public class Multiply6{
    public static void main (String[] args) throws IOException{
        //creating new InputStreamReader
        InputStreamReader is = new InputStreamReader (System.in);
        //creating new BufferedReader Class
        BufferedReader br = new BufferedReader (is);
        
        /*Method-2:
        Using BufferedReader and InputStreamReader in single command:
        BufferedReader in = new BufferedReader (new InputSreamReader (System.in));
        */
        
        System.out.println ("Please enter first number: ");
        int num1 = Integer.parseInt (br.readLine());
        System.out.println ("Please enter second number: ");
        int num2 = Integer.parseInt (br.readLine());
        
        //compute multiplication
        int result = num1*num2;
        
        //printing result
        System.out.println ("The multiplication of "+num1+" and "+num2+" = "+result);
    }
}


Save this file as 'Multiply6.java'
Compile: $javac Multiply6.java
Execute: $java Multiply6
Output:

Please enter first number:
12
Please enter second number:
5
The multiplication of 12 and 5 = 60