-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP01_SwapTwoNumbers.java
More file actions
45 lines (36 loc) · 1.43 KB
/
Copy pathP01_SwapTwoNumbers.java
File metadata and controls
45 lines (36 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
package programs;
/**
* ============================================================
* PROGRAM 01: Swap Two Numbers
* ============================================================
* Problem: Write a Java Program (WAP) to swap two numbers:
* a) Using a temporary third variable
* b) Without using a third variable (arithmetic trick)
* c) Using bitwise XOR operator
* ============================================================
*/
public class P01_SwapTwoNumbers {
public static void main(String[] args) {
System.out.println("=== 1. USING A THIRD (TEMP) VARIABLE ===");
int a = 10, b = 20;
System.out.printf("Before Swap: a = %d, b = %d%n", a, b);
int temp = a;
a = b;
b = temp;
System.out.printf("After Swap : a = %d, b = %d%n%n", a, b);
System.out.println("=== 2. WITHOUT A THIRD VARIABLE (ARITHMETIC) ===");
int x = 50, y = 100;
System.out.printf("Before Swap: x = %d, y = %d%n", x, y);
x = x + y; // x becomes 150
y = x - y; // y becomes 50
x = x - y; // x becomes 100
System.out.printf("After Swap : x = %d, y = %d%n%n", x, y);
System.out.println("=== 3. USING BITWISE XOR (FASTEST) ===");
int p = 7, q = 3;
System.out.printf("Before Swap: p = %d, q = %d%n", p, q);
p = p ^ q;
q = p ^ q;
p = p ^ q;
System.out.printf("After Swap : p = %d, q = %d%n", p, q);
}
}