Skip to content

Commit 69ce44e

Browse files
committed
Create add-two-integers.mdx
1 parent b749090 commit 69ce44e

File tree

1 file changed

+54
-0
lines changed

1 file changed

+54
-0
lines changed
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
---
2+
title: Java Program to Add Two Integers
3+
shortTitle: Add Two Integers
4+
description: In this program, you'll learn to store and add two integer numbers in Java. After addition, the final sum is displayed on the screen.
5+
---
6+
7+
import { List, ListItemGood } from '@/components/List'
8+
9+
10+
The integer is stored in a variable using `System.in`, and is displayed on the screen using `System.out`.
11+
12+
To understand this example, you should have the knowledge of the following Java programming topics:
13+
14+
- [Java Operators](/docs/operators)
15+
- [Java Basic Input and Output](/docs/basic-input-output)
16+
17+
## 1. Add Two Integers
18+
19+
A Java program that add two numbers predefined by the user is as follows:
20+
21+
### Example: Program to Add Two Integers
22+
23+
#### Input
24+
25+
```java
26+
class Main {
27+
28+
public static void main(String[] args) {
29+
30+
System.out.println("Enter two numbers");
31+
int first = 10;
32+
int second = 20;
33+
34+
System.out.println(first + " " + second);
35+
36+
// add two numbers
37+
int sum = first + second;
38+
System.out.println("The sum is: " + sum);
39+
}
40+
}
41+
```
42+
43+
#### Output
44+
45+
```text
46+
Enter two numbers
47+
10 20
48+
The sum is: 30
49+
```
50+
In this program, two integers `10` and `20` are stored in integer variables `first` and `second` respectively.
51+
52+
Then, `first` and `second` are added using the `+` operator, and its result is stored in another variable `sum`.
53+
54+
Finally, `sum` is printed on the screen using `println()` function.

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