Java program for Multiplication two Integers
In this section, you will learn how to easily multiply two numbers in Java. Let's see :
If you using int data type, then you can multiply numbers up to a limit(range of int data type).
If you want to multiply very large numbers, then you may use the BigInteger class in Java.
1.) First program :
In this program, we specify the value of both the numbers in the program itself.
Let's try to create a simple example :
➤ Example : multiply two numbers with predefined value;
class MultiplyTwoNumbers
{
public static void main(String args[]) {
int x = 15, y = 10, multiply;
multiply = x * y;
System.out.println("Multiplication of two numbers is : "+multiply);
}
}
Explanation :
Example 1 : explanation
Then, x and y are multiply using the * operator, and its result is stored in another variable multiply.
Finally, multiply is printed on the screen using System.out.println() function.
In the above program, we have created three integer variables x, y and multiply, then stored (15 and 10) values in x and y.
2.) Second program :
In this program, we will take two inputs from the user(entered by the user) and prints the multiply.
Note : Scanner class is a part of package(java.util), so we required to import this package in our Java program.
Let's try to create a simple example :
➤ Example : multiply two numbers with user input(Scanner);
import java.util.Scanner;
class MultiplyTwoNumbers
{
public static void main(String args[]) {
int x, y, multiply;
System.out.println("Enter two integers to calculate their Multiplication : ");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
multiply = x * y;
System.out.println("Multiplication of two numbers is : "+multiply);
}
}
Explanation :
Example 2 : explanation
Then, x and y are multiply using the * operator, and its result is stored in another variable multiply.
Finally, multiply is printed on the screen using System.out.println() function.
In the above program, we have created three integer variables x, y and multiply, then create a Scanner class so that you can take input from the user for x and y.
Comments