Skip to content

Java program to multiply two numbers #371

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/navs/program.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,6 @@ export const programsNav = {
pages['check-even-or-odd'],
pages['java-program-to-add-two-binary-numbers'],
pages['java-program-to-add-two-complex-numbers'],
pages['multiply-two-numbers'],
],
}
108 changes: 108 additions & 0 deletions src/pages/programs/multiply-two-numbers.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
---
title: Java Program to Multiply Two Numbers
shortTitle: Multiply Two Numbers
description: In this program, you'll learn to store and multiply two integer numbers in Java. After multiplication, the final value is displayed on the screen.
---

import { TipInfo } from '@/components/Tip'

To understand this example, you should have the knowledge of the following Java programming topics:

- [Java Operators](/docs/operators)
- [Java Basic Input and Output](/docs/basic-input-output)

## Multiply Two Numbers

A Java program that multiply two numbers given by the user is as follows:

### Example 1: Program to Multiply Two Integers

```java
import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // create an object of Scanner

System.out.print("Enter two numbers: "); // take input from the user
int first = input.nextInt();
int second = input.nextInt();

System.out.println("Entered numbers are: " + first + " " + second);

// multiply two numbers
int multiplication = first * second;
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);

input.close();
}
}
```

#### Output

```text
Enter two numbers: 10 2
Entered numbers are: 10 2
Multiplication: 10 * 2 = 20
```

Just like the [previous program](/programs/add-two-integers#example-program-to-add-two-integers) of addition,

`first` and `second` are multiplied using the `*` operator, and its result is stored in another variable `multiplication`.

And then, `multiplication` along with some [string concatenation](/docs/basic-input-output#example-print-concatenated-strings) is printed on the screen using `println()` function.

<TipInfo>

Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)

Here two input numbers are taken from user one after another with space in between them which distinguish between two different inputs, this useful behavior is because of the default settings of Scanner called as Delimiter, [learn more here](https://www.javatpoint.com/post/java-scanner-delimiter-method).

</TipInfo>

### Example 2: Program to Multiply Two Decimal Numbers

*Multiplication can be performed between positive, negative numbers as well.*

```java
import java.util.Scanner;

class Main {

public static void main(String[] args) {

Scanner input = new Scanner(System.in); // create an object of Scanner

System.out.print("Enter two numbers: "); // take input from the user
double first = input.nextDouble();
double second = input.nextInt();

System.out.println("Entered numbers are: " + first + " " + second);

// multiply two numbers
double multiplication = first * second;
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);

input.close();
}
}
```

#### Output

```text
Enter two numbers: -13.0 2.3
Entered numbers are: -13.0 2.3
Multiplication: -13.0 * 2.3 = -29.9
```

Datatype of variable `multiplication` is `double`, because the result of multiplying two numbers having data type `double` is `double`.

<TipInfo>

**Note:** Here, the output `multiplication` is negative, because these operations follow [standard mathematical rules](https://en.wikipedia.org/wiki/Multiplication#Properties).

</TipInfo>
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy