CodeGym/Java Blog/Java Examples/Java Program to Multiply Two Numbers
Author
Pavlo Plynko
Java Developer at CodeGym

Java Program to Multiply Two Numbers

Published in the Java Examples group
members
There are special operators reserved for arithmetic operations in Java, and they do not differ from those generally accepted in computer science. In particular, the * operator is used to multiply two numbers. Java has several primitive data types that represent numbers. They differ in size, or rather, in the amount of memory allocated for them, as well as in whether they are integers (int, byte, short, long) or fractional (double, float). You can multiply any two of these primitive data types, as in mathematics, we can multiply with each other any numbers of different sizes, fractional and non-fractional.
int a = 5; int b = 10; int c = a*b; double x = 1.2; double y = a*x;
Let's look at some examples of multiplying two numbers in Java. Example 1. Multiplication of two integers
public class MultiplyExample {
   public static void main(String[] args) {
       int a;
       int b;
       int c;
       a = 5;
       b = 58;
       c = a*b; //integer number to keep the result of multiplication
       System.out.println("5*58 = " + c);
   }
}
The output is:
5*58 = 290
In fact, you can multiply two integers explicitly without assigning their values to a variable, and display the result of the action on the screen, or multiply the number by a variable: Example 2. Multiplication of numbers.
public class MultiplyExample {
   public static void main(String[] args) {
       int a;
       a = 5;
       System.out.println("7*7 = " + 7*7);
       System.out.println("a*5 = " + a*5);
   }
}
And here is the output:
7*7 = 49 a*5 = 25
You can also multiply fractional numbers by other fractional numbers or fractional numbers by integers. Please note that the result of the operation of multiplying a fractional by an integer will be of the fractional type. To do this kind of multiplication, Java casts a primitive integer type, for example, int, to the type of the fractional number with which it is multiplied (for example, double), and the result will also be double.
public class MultiplyExample2 {
   public static void main(String[] args) {
       double x = 15.7;
       double y = 2.1;
       int a = 3;
       double z = x*y;
       double b = a*x;
       //if you try something like int s = a*x; your program won't run, it's a mistake.
       System.out.println(x + "*" + y + " = " + z);
       System.out.println(a + "*" + x + " = " + b);
   }
}
Multiplication is a very simple operation, but it must be used with care. For example, you can assign a multiplication result type that is too short, and the result simply will not fit into this variable. Let's take the variable short equal to 32767. This is the upper limit value for a variable of this type (the number 32768 can no longer be short, since it does not fit into the 2 bytes allotted for this data type). Let's consider an example:
public class MultiplyExample3 {
   public static void main(String[] args) {
       short myShort1 = 32767;
       short myShort2 = 2;
       short myShort3 = myShort1*myShort2;
   }
}
A modern IDE, such as IDEA, will underline the line in which the variable myShort3 is defined in red, but if we do run the program, we get the following error message:
Error:(5, 34) java: incompatible types: possible lossy conversion from int to short
So when you write your program, try to think about whether this or that data type is enough for you. In the case of the example above, int is suitable. You can also to write a more general-purpose program with user input of two numbers:
import java.util.Scanner;

public class MultiplyExample3 {

       public static void main(String[] args) {

           Scanner scanner = new Scanner(System.in);
           System.out.print("Enter first number = ");
           double myDouble1 = scanner.nextDouble();
           System.out.print("Enter second number =  ");
           double myDouble2 = scanner.nextDouble();
           scanner.close();

           double result = myDouble1*myDouble2;

           // Displaying the multiplication result
           System.out.println(myDouble1 + "*" + myDouble2 + " = " + result);
       }
   }
Here is the result:
Enter first number = 5 Enter second number = 12 5.0*12.0 = 60.0
Comments
  • Popular
  • New
  • Old
You must be signed in to leave a comment
This page doesn't have any comments yet