How to Multiply Two Numbers in Java

Simple programming problems such as multiplication of numbers is a good way to teach programming syntax without students getting overwhelmed by the programming solution. The following Java program demonstrates multiplication of two numbers. This is a trivial example program, however it shows how to handle input and output, performing calculations and the use of library classes such as Scanner in Java language.

The user is prompted to enter first and the second number. Product of the entered numbers is calculated and is displayed on the command line in an equation form.

import java.util.Scanner;

/**
 * Java program for multiplying two numbers
 * @author jj
 */
public class MultiplyNumbers {
    public static void main(String[] args) {
        Scanner sn = new Scanner(System.in);
        
        System.out.print("Please enter first number:");
        double n1 = sn.nextDouble();                
        System.out.print("Please enter second number:");
        double n2 = sn.nextDouble();
        
        double product = n1 * n2;
        System.out.println(n1+"*"+n2+"="+product);
        
    }
}