Skip to content

Commit 66d5e2b

Browse files
Merge branch 'main' into feat/programs-check-prime
2 parents 7227539 + 713b6a0 commit 66d5e2b

3 files changed

Lines changed: 129 additions & 14 deletions

File tree

content/programs/contents.mdx

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,14 @@ description: A collection of basic Java programs categorized by topic.
88
1. [Print an Integer](/programs/print-an-integer)
99
2. [Add Two Integers](/programs/add-two-integers)
1010
3. [Check Even or Odd Number](/programs/check-even-or-odd)
11-
4. [Check Prime Number](/programs/java-program-to-check-prime-number)
12-
5. [Add Two Binary](/programs/java-program-to-add-two-binary-numbers)
13-
6. [Add Two Complex Numbers](/programs/java-program-to-add-two-complex-numbers)
14-
7. [Multiply Two Numbers](/programs/multiply-two-numbers)
15-
8. [Check Leap Year](/programs/java-program-to-check-Leap-year)
16-
9. [Calculate Simple interest](/programs/calculate-simple-interest)
17-
10. [Check Divisibility](/programs/java-program-to-check-divisbility)
18-
11. [Calculate Quotient and Reminder](/programs/find-quotient-and-reminder)
19-
12. [Calculate Power of a Number](/programs/calculate-power-of-a-number)
20-
13. [Calculate Compound Interest](/programs/calculate-compound-interest)
21-
14. [Calculate Factorial of a Number](/programs/factorial-in-java)
22-
15. [Swap Two Numbers](/programs/swap-two-numbers)
11+
4. [Add Two Binary](/programs/java-program-to-add-two-binary-numbers)
12+
5. [Add Two Complex Numbers](/programs/java-program-to-add-two-complex-numbers)
13+
6. [Multiply Two Numbers](/programs/multiply-two-numbers)
14+
7. [Check Leap Year](/programs/java-program-to-check-Leap-year)
15+
8. [Calculate Simple interest](/programs/calculate-simple-interest)
16+
9. [Check Divisibility](/programs/java-program-to-check-divisbility)
17+
10. [Calculate Quotient and Reminder](/programs/find-quotient-and-reminder)
18+
11. [Calculate Power of a Number](/programs/calculate-power-of-a-number)
19+
12. [Calculate Compound Interest](/programs/calculate-compound-interest)
20+
13. [Calculate Factorial of a Number](/programs/factorial-in-java)
21+
14. [Swap Two Numbers](/programs/swap-two-numbers)

content/programs/meta.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@
66
"index",
77
"contents",
88
"---Basic I/O Programs---",
9-
"print-an-integer",
10-
"add-two-integers",
9+
"print-an-integer",
10+
"add-two-integers",
11+
"swap-two-numbers",
1112
"multiply-two-numbers",
1213

1314
"---Mathematical Calculations---",
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
---
2+
title: Java Program to Swap Two Numbers
3+
shortTitle: Swap Two Numbers
4+
description: Learn different ways to swap two numbers in Java — using a temporary variable, arithmetic operations, and bitwise XOR — with clear examples and outputs.
5+
---
6+
7+
Swapping two numbers is a common beginner-friendly exercise in Java. Below are three simple ways to do it:
8+
9+
- Using a temporary variable (safest and most readable)
10+
- Using arithmetic operations (beware of overflow for very large numbers)
11+
- Using bitwise XOR (no temp variable; works for integers)
12+
13+
Before you start, you may want to review:
14+
15+
- [Java Variables and Literals](/docs/variables-and-literals)
16+
- [Java Operators](/docs/operators)
17+
- [Java Basic Input and Output](/docs/basic-input-output)
18+
19+
## 1) Swap Using a Temporary Variable
20+
21+
This is the most straightforward and recommended approach.
22+
23+
### Example
24+
25+
```java
26+
class Main {
27+
public static void main(String[] args) {
28+
int a = 10;
29+
int b = 20;
30+
31+
System.out.println("Before swap: a = " + a + ", b = " + b);
32+
33+
int temp = a; // store a
34+
a = b; // put b into a
35+
b = temp; // put original a into b
36+
37+
System.out.println("After swap: a = " + a + ", b = " + b);
38+
}
39+
}
40+
```
41+
42+
#### Output
43+
44+
```plaintext
45+
Before swap: a = 10, b = 20
46+
After swap: a = 20, b = 10
47+
```
48+
49+
## 2) Swap Without Temp (Arithmetic)
50+
51+
Uses addition and subtraction. Note: This can overflow if the numbers are near the integer limits.
52+
53+
### Example
54+
55+
```java
56+
class Main {
57+
public static void main(String[] args) {
58+
int a = 15;
59+
int b = 27;
60+
61+
System.out.println("Before swap: a = " + a + ", b = " + b);
62+
63+
a = a + b; // a becomes sum
64+
b = a - b; // b becomes original a
65+
a = a - b; // a becomes original b
66+
67+
System.out.println("After swap: a = " + a + ", b = " + b);
68+
}
69+
}
70+
```
71+
72+
#### Output
73+
74+
```plaintext
75+
Before swap: a = 15, b = 27
76+
After swap: a = 27, b = 15
77+
```
78+
79+
## 3) Swap Using XOR (No Temp)
80+
81+
Works for integers and avoids extra memory, but is less readable for beginners.
82+
83+
### Example
84+
85+
```java
86+
class Main {
87+
public static void main(String[] args) {
88+
int a = 5;
89+
int b = 9;
90+
91+
System.out.println("Before swap: a = " + a + ", b = " + b);
92+
93+
a = a ^ b; // Step 1
94+
b = a ^ b; // Step 2: now b is original a
95+
a = a ^ b; // Step 3: now a is original b
96+
97+
System.out.println("After swap: a = " + a + ", b = " + b);
98+
}
99+
}
100+
```
101+
102+
#### Output
103+
104+
```plaintext
105+
Before swap: a = 5, b = 9
106+
After swap: a = 9, b = 5
107+
```
108+
109+
---
110+
111+
### Which Method Should You Use?
112+
113+
- Prefer the temporary variable method for clarity and safety.
114+
- The arithmetic method can be a neat trick, but avoid it when values may overflow.
115+
- XOR is an interesting technique; use it only when it adds real value and readability is not sacrificed.

0 commit comments

Comments
 (0)