BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Program to Multiply Two Numbers

Last Updated: September 8, 2017 by Chaitanya Singh | Filed Under: Java Examples

When you start learning java programming, you get these type of problems in your assignment. Here we will see two Java programs, first program takes two integer numbers (entered by user) and displays the product of these numbers. The second program takes any two numbers (can be integer or floating point) and displays the result.

Example 1: Program to read two integer and print product of them

This program asks user to enter two integer numbers and displays the product. To understand how to use scanner to take user input, checkout this program: Program to read integer from system input.

import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {

        /* This reads the input provided by user
         * using keyboard
         */
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first number: ");

        // This method reads the number provided using keyboard
        int num1 = scan.nextInt();
        
        System.out.print("Enter second number: ");
        int num2 = scan.nextInt();

        // Closing Scanner after the use
        scan.close();
        
        // Calculating product of two numbers
        int product = num1*num2;
        
        // Displaying the multiplication result
        System.out.println("Output: "+product);
    }
}

Output:

Enter first number: 15
Enter second number: 6
Output: 90

Example 2: Read two integer or floating point numbers and display the multiplication

In the above program, we can only integers. What if we want to calculate the multiplication of two float numbers? This programs allows you to enter float numbers and calculates the product.

Here we are using data type double for numbers so that you can enter integer as well as floating point numbers.

import java.util.Scanner;

public class Demo {

    public static void main(String[] args) {

        /* This reads the input provided by user
         * using keyboard
         */
        Scanner scan = new Scanner(System.in);
        System.out.print("Enter first number: ");

        // This method reads the number provided using keyboard
        double num1 = scan.nextDouble();
        
        System.out.print("Enter second number: ");
        double num2 = scan.nextDouble();

        // Closing Scanner after the use
        scan.close();
        
        // Calculating product of two numbers
        double product = num1*num2;
        
        // Displaying the multiplication result
        System.out.println("Output: "+product);
    }
}

Output:

Enter first number: 1.5
Enter second number: 2.5
Output: 3.75

Top Related Articles:

  1. Java Program to Calculate average using Array
  2. Java Program to Add two Binary Numbers
  3. Neon Number in Java with example
  4. Java Program to Add two Numbers
  5. Sunny Number Program in Java

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

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

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap