Skip to content

Commit e188783

Browse files
authored
Merge pull request #371 from AbhishekJadhav2002/java-program-to-multiply-two-numbers
Java program to multiply two numbers
2 parents 5bae452 + a7c48d6 commit e188783

File tree

2 files changed

+109
-0
lines changed

2 files changed

+109
-0
lines changed

src/navs/program.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export const programsNav = {
1313
pages['check-even-or-odd'],
1414
pages['java-program-to-add-two-binary-numbers'],
1515
pages['java-program-to-add-two-complex-numbers'],
16+
pages['multiply-two-numbers'],
1617
],
1718
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
---
2+
title: Java Program to Multiply Two Numbers
3+
shortTitle: Multiply Two Numbers
4+
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.
5+
---
6+
7+
import { TipInfo } from '@/components/Tip'
8+
9+
To understand this example, you should have the knowledge of the following Java programming topics:
10+
11+
- [Java Operators](/docs/operators)
12+
- [Java Basic Input and Output](/docs/basic-input-output)
13+
14+
## Multiply Two Numbers
15+
16+
A Java program that multiply two numbers given by the user is as follows:
17+
18+
### Example 1: Program to Multiply Two Integers
19+
20+
```java
21+
import java.util.Scanner;
22+
23+
class Main {
24+
25+
public static void main(String[] args) {
26+
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();
32+
33+
System.out.println("Entered numbers are: " + first + " " + second);
34+
35+
// multiply two numbers
36+
int multiplication = first * second;
37+
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);
38+
39+
input.close();
40+
}
41+
}
42+
```
43+
44+
#### Output
45+
46+
```text
47+
Enter two numbers: 10 2
48+
Entered numbers are: 10 2
49+
Multiplication: 10 * 2 = 20
50+
```
51+
52+
Just like the [previous program](/programs/add-two-integers#example-program-to-add-two-integers) of addition,
53+
54+
`first` and `second` are multiplied using the `*` operator, and its result is stored in another variable `multiplication`.
55+
56+
And then, `multiplication` along with some [string concatenation](/docs/basic-input-output#example-print-concatenated-strings) is printed on the screen using `println()` function.
57+
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>
65+
66+
### Example 2: Program to Multiply Two Decimal Numbers
67+
68+
*Multiplication can be performed between positive, negative numbers as well.*
69+
70+
```java
71+
import java.util.Scanner;
72+
73+
class Main {
74+
75+
public static void main(String[] args) {
76+
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();
82+
83+
System.out.println("Entered numbers are: " + first + " " + second);
84+
85+
// multiply two numbers
86+
double multiplication = first * second;
87+
System.out.println("Multiplication: " + first + " * " + second + " = " + multiplication);
88+
89+
input.close();
90+
}
91+
}
92+
```
93+
94+
#### Output
95+
96+
```text
97+
Enter two numbers: -13.0 2.3
98+
Entered numbers are: -13.0 2.3
99+
Multiplication: -13.0 * 2.3 = -29.9
100+
```
101+
102+
Datatype of variable `multiplication` is `double`, because the result of multiplying two numbers having data type `double` is `double`.
103+
104+
<TipInfo>
105+
106+
**Note:** Here, the output `multiplication` is negative, because these operations follow [standard mathematical rules](https://en.wikipedia.org/wiki/Multiplication#Properties).
107+
108+
</TipInfo>

0 commit comments

Comments
 (0)
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