Java Program to Multiply two Numbers

In this article, you will learn how to write a Java program to multiply two numbers. Here, you will see multiple solutions for it such as multiplying two static numbers, multiplying two dynamically given numbers, and multiplying two Numbers using command-line arguments.

Multiplication of two numbers in java

The multiplication of two numbers in java is very simple. Let’s assume we have two integer numbers x=5 and y=12. Now we will take a multiplication integer variable to calculate the multiplication of x and y. Let’s see the code for it below:

public class MultiplyTwoNumbers {

	public static void main(String[] args) {
		
		// declare and initialize two integer numbers
		int x=5;
		int y=12;

		// declare another variable to hold the multiplication of both numbers
		int multiplication;

		// calculate the multiplication
		multiplication = x * y;

		// print the calculated value
		System.out.println("The Multiplication of two Numbers: " + multiplication );

	}

}

OUTPUT:

The Multiplication of two Numbers: 60

Multiply two dynamically given Numbers in Java

Here we will take two integer numbers from the user dynamically (at run-time). To take input at run-time, we can take the Scanner class object for reading the user’s inputs. The Scanner class is used to get user input, and it is found in the “java.util” package. Let’s see the complete code for it below:

import java.util.Scanner;

public class MultiplyTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, multiplication;

		// create scanner object to take dynamic input
		Scanner sc = new Scanner(System.in);

		System.out.print("Enter first number: ");
		// taking first number
		x = sc.nextInt();

		System.out.print("Enter second number: ");
		// taking second number
		y = sc.nextInt();

		// calculate the multiplication
		multiplication = x * y;

		// print the calculated value
		System.out.println("The Multiplication of the given two Numbers: " + multiplication);

	}

}

OUTPUT:

Enter first number: 10
Enter second number: 5
The Multiplication of the given two Numbers: 50

Multiply two Numbers in Java using command-line Arguments

Now we will see, the multiplication of two numbers that are given from command line arguments. Generally, the inputs that come from the command line arguments are in the String datatype. They are stored as strings in the String array passed to the args[] parameter of the main() method in Java. So we have to convert the string to an integer using the parseInt() method before storing it in integer datatype. The command line arguments should be passed to the main() method at run-time.

Let’s see the code for it below:

public class MultiplyTwoNumbers {

	public static void main(String[] args) {

		// declare required variables
		int x, y, multiplication;
        
        // read first number
		x = Integer.parseInt(args[0]);

        // read second number
		y = Integer.parseInt(args[1]);

		// calculate the multiplication
		multiplication = x * y;

		// print the calculated value
		System.out.println("The Multiplication of given two Numbers: " + multiplication);

	}

}

OUTPUT:

The Multiplication of two Numbers: 150

In order to execute the above program, we will open the command prompt and compile it first with the command >javac MultiplyTwoNumbers.java then run the command >java MultiplyTwoNumbers 10 15

Java logical programs list


Java Basic Programs

Java String Programs

Java Programs based on the Collection Framework

Leave a Reply

Your email address will not be published. Required fields are marked *