Skip to content

Commit da2ac2a

Browse files
Resolved suggested changes in /pages/programs/multiply-two-numbers.mdx
1 parent 92524f9 commit da2ac2a

2 files changed

Lines changed: 40 additions & 21 deletions

File tree

src/navs/program.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ export const programsNav = {
1010
Introduction: [
1111
pages['print-an-integer'],
1212
pages['add-two-integers'],
13-
pages['multiply-two-numbers'],
1413
pages['check-even-or-odd'],
1514
pages['java-program-to-add-two-binary-numbers'],
1615
pages['java-program-to-add-two-complex-numbers'],
16+
pages['multiply-two-numbers'],
1717
],
1818
}
Lines changed: 39 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,89 +1,108 @@
11
---
2-
title: Java Program to Multiply Two Integers
3-
shortTitle: Multiply Two Integers
2+
title: Java Program to Multiply Two Numbers
3+
shortTitle: Multiply Two Numbers
44
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.
55
---
66

7-
import { List, ListItemGood } from '@/components/List'
8-
7+
import { TipInfo } from '@/components/Tip'
98

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

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

15-
## Multiply Two Integers
14+
## Multiply Two Numbers
1615

17-
A Java program that multiply two numbers predefined by the user is as follows:
16+
A Java program that multiply two numbers given by the user is as follows:
1817

1918
### Example 1: Program to Multiply Two Integers
2019

2120
```java
21+
import java.util.Scanner;
22+
2223
class Main {
2324

2425
public static void main(String[] args) {
2526

26-
System.out.println("Enter two numbers");
27-
int first = 10;
28-
int second = 2;
27+
Scanner input = new Scanner(System.in); // create an object of Scanner
28+
29+
System.out.print("Enter two numbers: "); // take input from the user
30+
int first = input.nextInt();
31+
int second = input.nextInt();
2932

30-
System.out.println(first + " " + second);
33+
System.out.println("Entered numbers are: " + first + " " + second);
3134

3235
// multiply two numbers
3336
int multiplication = first * second;
3437
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);
38+
39+
input.close();
3540
}
3641
}
3742
```
3843

3944
#### Output
4045

4146
```text
42-
Enter two numbers
43-
10 2
47+
Enter two numbers: 10 2
48+
Entered numbers are: 10 2
4449
Multiplication: 10 * 2 = 20
4550
```
51+
4652
Just like the [previous program](/programs/add-two-integers#example-program-to-add-two-integers) of addition,
4753

48-
Then, `first` and `second` are multiplied using the `*` operator, and its result is stored in another variable `multiplication`.
54+
`first` and `second` are multiplied using the `*` operator, and its result is stored in another variable `multiplication`.
4955

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

58+
<TipInfo>
59+
60+
Don't know how to take input from the user ? Look at [this examples](/docs/basic-input-output#java-input)
61+
62+
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).
63+
64+
</TipInfo>
5265

5366
### Example 2: Program to Multiply Two Double
5467

5568
Multiplication can be performed between positive, negative numbers as well,
5669

5770
```java
71+
import java.util.Scanner;
72+
5873
class Main {
5974

6075
public static void main(String[] args) {
6176

62-
System.out.println("Enter two numbers");
63-
double first = -13.0;
64-
double second = 2.3;
77+
Scanner input = new Scanner(System.in); // create an object of Scanner
78+
79+
System.out.print("Enter two numbers: "); // take input from the user
80+
double first = input.nextDouble();
81+
double second = input.nextInt();
6582

66-
System.out.println(first + " " + second);
83+
System.out.println("Entered numbers are: " + first + " " + second);
6784

6885
// multiply two numbers
6986
double multiplication = first * second;
7087
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);
88+
89+
input.close();
7190
}
7291
}
7392
```
7493

7594
#### Output
7695

7796
```text
78-
Enter two numbers
79-
-13.0 2.3
97+
Enter two numbers: -13.0 2.3
98+
Entered numbers are: -13.0 2.3
8099
Multiplication: -13.0 * 2.3 = -29.9
81100
```
82101

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

85104
<TipInfo>
86105

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

89108
</TipInfo>

0 commit comments

Comments
 (0)