Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Saturday, November 23, 2019

Java Program to Multiply Two Numbers (Integer, Floating and Double numbers)

1. Overview


In this article, You'll learn to a java program to multiply two numbers. This is a very easy and basic program for freshers and who just started learning java programming language. This program must be part of your assessment in your training. We will show you the two programs first is how to multiply and calculate two integer numbers and in second, will be showing multiplying two floating or double numbers. In both cases, numbers are entered by the user using Scanner.

The formula is using asterisk symbol '*' first_number * second_number.







2. Java Program to Multiply Two Integer Numbers


The below code is to read the two int numbers from the user and display the result on the console using firstNumber * secondNumber formula. You can check java program to add two numbers.

package com.java.w3schools.blog.java.program.to;

/**
 * 
 * JavaProgramTo.com
 * 
 * Java Program to Multiply Two Numbers
 * 
 */
import java.util.Scanner;

public class MultiplyTwoInts {

 public static void main(String[] args) {

  // scanner is to read the input from keyboard
  Scanner s = new Scanner(System.in);
  System.out.println("Enter two integer numbers: ");

  // reading first int number.
  int firstNumber = s.nextInt();

  // reading second int number.
  int secondNumber = s.nextInt();

  // calculating the product of the two int numbers.
  int multiplicationResult = firstNumber * secondNumber;

  // printing the final product.
  System.out.println("Final multiplication result : " + multiplicationResult);
  
  // This is optional to close scanner.
  s.close();
 }

}

Output:

Enter two integer numbers: 
12
10
Final multiplication result : 120

Entered two int numbers such as 12 and 10. After, executing the product logic then displayed 120 as a final result.

2. Java Program to Multiply Two Floating or double Numbers (Entered by user)


This is similar to the above approach but the user enters the floating numbers whereas the above code produces the accurate results only for integer values. We are now seeing how to calculate and find the floating decimal points multiplication. To read the floating-point values from the user, we need to use nextFloat() method rather than nextInt() method.

Let us take a look at the below program.

package com.java.w3schools.blog.java.program.to;

/**
 * 
 * JavaProgramTo.com
 * 
 * Java Program to Multiply Two Floating Numbers
 * 
 */
import java.util.Scanner;

public class MultiplyTwoFloating {

 public static void main(String[] args) {

  // scanner is to read the input from keyboard
  Scanner s = new Scanner(System.in);
  System.out.println("Enter two floating numbers: ");

  // reading first float number.
  float firstNumber = s.nextFloat();

  // reading second float number.
  float secondNumber = s.nextFloat();

  // calculating the product of the two int numbers.
  float multiplicationResult = firstNumber * secondNumber;

  // printing the final product.
  System.out.println("Final multiplication result : " + multiplicationResult);

  // This is optional to close scanner.
  s.close();
 }

}

Output:

Enter two floating numbers: 
1.5
3.0
Final multiplication result : 4.5

Running the same program for different decimal inputs.

Enter two floating numbers: 
7.5
12.4
Final multiplication result : 93.0

If you want to work with double values then we should use nextDouble() method when reading the inputs from the user.

4. Conclusion


In this article, We have seen how to calculate the product of two numbers. Shown the example java programs for integer, float and double values. All examples take inputs from user-entered from keyboard.

No comments:

Post a Comment

Please do not add any spam links in the comments section.