Java Program to Multiply Two Numbers (With Full Explanation)
Program
import [Link];
public class MultiplyNumbers {
public static void main(String[] args) {
Scanner sc = new Scanner([Link]);
[Link]("Enter first number: ");
int num1 = [Link]();
[Link]("Enter second number: ");
int num2 = [Link]();
int result = num1 * num2;
[Link]("Result = " + result);
[Link]();
}
}
Explanation of Every Line
import [Link];
• Imports the Scanner class from [Link] package.
• Scanner allows reading input from keyboard.
• Required for nextInt(), nextLine(), etc.
public class MultiplyNumbers {
• Declares a public class named MultiplyNumbers.
• Java programs must be inside a class.
• The file name must match the class name.
public static void main(String[] args) {
• Starting point of the Java program.
• Execution begins from the main method.
Scanner sc = new Scanner([Link]);
• Creates a Scanner object named sc.
• [Link] connects Scanner to the keyboard.
[Link]("Enter first number: ");
• Prints a message asking for the first number.
• print() does not move to a new line.
int num1 = [Link]();
• Reads an integer value from the user.
• Stores it in num1.
[Link]("Enter second number: ");
• Asks the user to enter the second number.
int num2 = [Link]();
• Reads the second integer.
• Stores it in num2.
int result = num1 * num2;
• Multiplies num1 and num2.
• Stores the product in result.
[Link]("Result = " + result);
• Displays the result.
• println() prints and moves to next line.
[Link]();
• Closes the Scanner object.
• Good practice to release resources.