Introduction
In this tutorial, we will learn how to write a simple Java program to multiply two numbers. We will explore two different approaches for multiplying two numbers:
- Without User Input: In this approach, we will assign two numbers directly to variables inside the Java program.
- With User Input Using Scanner: In this approach, the program accepts two numbers from the user at runtime and calculates their product.
Multiplication is one of the basic arithmetic operations in Java. The multiplication operator (*) is used to multiply two numeric values.
Let’s explore both approaches step by step.
Learning Objectives
By the end of this tutorial, you will be able to:
- Understand the basic structure and execution flow of a Java program.
- Declare and initialize numeric variables to store values.
- Apply the multiplication operator (*) to perform arithmetic operations.
- Write a Java program using hardcoded (static) values.
- Capture user input dynamically at runtime using the Scanner class.
- Calculate and display the resulting product in the console.
Java Program to Multiply Two Numbers Without User Input
Problem Statement
Write a Java program to multiply two numbers without taking input from the user. Store two integer values directly in variables, multiply them, and display the result on the console.
Formula Used
product = firstNumber * secondNumber
Input Format
No user input is required. The two numbers must be assigned directly to variables inside the Java program. For this example, use:
int firstNumber = 10; int secondNumber = 20;
Output Format
Display the two numbers and their product on the console.
First Number : 10 Second Number : 20 Product : 200
Constraints
- Do not use Scanner, BufferedReader, or command-line arguments.
- Store the numbers directly in variables.
- Use the int primitive data type for the numbers.
- Use the multiplication operator (*).
- The program must execute inside the public static void main(String[] args) method.
Expected Solution Approach
You can solve this program in four simple steps:
- Declare an integer variable named firstNumber and assign the value 10 to it.
- Declare an integer variable named secondNumber and assign the value 20 to it.
- Multiply firstNumber and secondNumber using the multiplication operator (*) and store the result in a variable named product.
- Display the product using the System.out.println() statement on the console.
Core Calculation Statement
int product = firstNumber * secondNumber;
Note: This basic approach executes sequentially inside the main method without requiring user input, loops, conditionals, arrays, or custom helper methods.
Complete Java Program to Multiply Two Numbers
public class MultiplyTwoNumbers {
public static void main(String[] args) {
// Step 1: Declare and initialize variables
int firstNumber = 10;
int secondNumber = 20;
// Step 2: Perform multiplication
int product = firstNumber * secondNumber;
// Step 3: Display the result
System.out.println("First Number : " + firstNumber);
System.out.println("Second Number : " + secondNumber);
System.out.println("Product : " + product);
}
}Console Output:
First Number : 10 Second Number : 20 Product : 200
Step-by-Step Code Explanation
Step 1: Define the Class
public class MultiplyTwoNumbers {In Java, executable code is written inside a class. Here, the class is named MultiplyTwoNumbers.
The public access modifier allows the class to be accessed from other classes. The class name follows Java’s PascalCase naming convention.
Step 2: Define the main() Method
public static void main(String[] args) {The main() method is the entry point of a standalone Java application. When the program is executed, the Java Virtual Machine (JVM) starts program execution from this method.
Step 3: Declare and Initialize the Variables
int firstNumber = 10; int secondNumber = 20;
Here, int specifies the primitive data type used to hold whole numbers (integers). firstNumber and secondNumber are variable names (identifiers) declared and initialized with values 10 and 20, respectively. The assignment operator (=) stores the value on the right into the variable on the left.
Step 4: Perform the Multiplication
int product = firstNumber * secondNumber;
The multiplication operator (*) multiplies the value stored in firstNumber by the value stored in secondNumber. Therefore, the output is 10 * 20 = 200. The resulting value 200 is stored in the product variable.
Step 5: Display the Output
System.out.println("First Number : " + firstNumber);
System.out.println("Second Number : " + secondNumber);
System.out.println("Product : " + product);The System.out.println() method displays the specified text and variable values on the console. The + operator is used here for string concatenation to combine the text with the values of the variables.
Java Program to Multiply Two Numbers Using Scanner
Problem Statement
Write a Java program that reads two integers from the user and calculates their product. The program should accept two numbers from the keyboard, multiply them using the multiplication operator (*), and display the result.
For example, if the user enters 15 and 10, the program should calculate 15 * 10 = 150.
Expected Solution Approach
You can solve this program in five simple steps:
- Import the Scanner class from the java.util package.
- Create a Scanner object to read input from the keyboard.
- Read two integer values from the user using scanner.nextInt().
- Multiply the two numbers using the multiplication operator (*) and store the result in the product variable.
- Display the entered numbers and calculated product, then close the Scanner object.
Complete Java Program to Multiply Two Numbers Using Scanner
import java.util.Scanner;
public class MultiplyUserInput {
public static void main(String[] args) {
// Step 1: Create Scanner object for user input
Scanner scanner = new Scanner(System.in);
// Step 2: Read input values from the user
System.out.print("Enter first number : ");
int firstNumber = scanner.nextInt();
System.out.print("Enter second number : ");
int secondNumber = scanner.nextInt();
// Step 3: Perform multiplication
int product = firstNumber * secondNumber;
// Step 4: Display the result
System.out.println("-------------------------");
System.out.println("First Number : " + firstNumber);
System.out.println("Second Number : " + secondNumber);
System.out.println("-------------------------");
System.out.println("Product : " + product);
// Step 5: Close the Scanner
scanner.close();
}
}Console Output:
Enter first number : 10 Enter second number : 20 ------------------------- First Number : 10 Second Number : 20 ------------------------- Product : 200
Step-by-Step Code Explanation
Step 1: Import the Scanner Class
import java.util.Scanner;
The Scanner class belongs to the java.util package. It is used to read input from sources such as the keyboard. Before using the Scanner class, we import it into the program.
Step 2: Create a Scanner Object
Scanner scanner = new Scanner(System.in);
Here, we created a Scanner object named scanner. System.in represents the standard input stream, which normally refers to keyboard input.
Step 3: Read the First Number
System.out.print("Enter first number : ");
int firstNumber = scanner.nextInt();The System.out.print() method asks the user to enter the first number. The scanner.nextInt() method reads an integer entered by the user and stores it in the firstNumber variable.
Step 4: Read the Second Number
System.out.print("Enter second number : ");
int secondNumber = scanner.nextInt();Similarly, the System.out.print() method asks the user to enter the second number. The entered integer is stored in the secondNumber variable.
Step 5: Multiply the Two Numbers
int product = firstNumber * secondNumber;
The multiplication operator (*) multiplies the two numbers. For example, if the firstNumber is 25 and the secondNumber is 4, the product is 25 * 4 = 100. The result is stored in the product variable.
Step 6: Display the Result
System.out.println("Product : " + product);The System.out.println() method displays the calculated product on the console.
Step 7: Close the Scanner
scanner.close();
The close() method closes the Scanner object after the input operation is complete. Closing the scanner is a good practice because it releases the resources associated with the input stream.
Difference Between the Two Approaches
| Approach | User Input | Main Concept |
|---|---|---|
| Without User Input | No | We assign values directly to variables. |
| Using a Scanner | Yes | We enter values at runtime. |
Without User Input
int firstNumber = 10; int secondNumber = 20; int product = firstNumber * secondNumber;
In this approach, we multiplied two predefined integer values without taking input from the user and then stored the result in a variable named product.
With User Input
int firstNumber = scanner.nextInt(); int secondNumber = scanner.nextInt(); int product = firstNumber * secondNumber;
In this approach, we used the Scanner class to read two numbers dynamically from the console at runtime, calculated their product, and displayed the result.
Follow-up Challenge
Challenge 1: Multiply Two Double Values Using User Input
After solving these programs, try modifying the program to read two double values from the user and calculate their product. For example:
Input:
25.5 10.25
Output:
Product = 261.375
Goal: Practice reading decimal values using Scanner, storing them in double variables, and performing multiplication using the multiplication operator (*).
Challenge 2: Multiply Three Numbers Using User Input
Modify the program so that it accepts three integers from the user and calculates their product. Read the three numbers from the user and multiply them together.
Calculation:
result = firstNumber * secondNumber * thirdNumber
For example, if the user enters:
10 5 2
The calculation should be:
10 * 5 * 2 = 100
Expected output:
Result = 100
Goal: Practice reading multiple values using Scanner, storing them in variables, and applying multiplication operations correctly.






