File tree Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Expand file tree Collapse file tree 1 file changed +54
-0
lines changed Original file line number Diff line number Diff line change
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.
You can’t perform that action at this time.
0 commit comments