-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwappingTwoNumbers.java
More file actions
48 lines (39 loc) · 1.02 KB
/
Copy pathSwappingTwoNumbers.java
File metadata and controls
48 lines (39 loc) · 1.02 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
46
47
48
package com;
public class SwappingTwoNumbers {
public static void main(String[] args) {
int a = 10, b = 20;
System.out.println("Before Swapping " + a + " " + b);
// Logic 1 -> Third Value
int t = a;
a = b;
b = t;
System.out.println("After Swapping " + a + " " + b);
/*
// Logic 2 -> Use + and = without using Third variable
a = a + b; //10+20=30
b = a - b; //30-20=10
a = a - b; //30-10=20
System.out.println("After Swapping " + a + " " + b);
*/
/*
// Logic 3 -> Use * and / without using Third variable
// Note: here a and b values should not be zero
a = a + b; //10*20=200
b = a - b; //200/20=10
a = a - b; //200/10=20
System.out.println("After Swapping " + a + " " + b);
*/
/*
// Logic 4 -> Bitwise Operator XOR(^)
a = a ^ b; //10^20=30
b = a ^ b; //30^20=10
a = a ^ b; //30^10=20
System.out.println("After Swapping " + a + " " + b);
*/
/*
// Logic 5 -> Single Statement a=10 b=20
b= a+b-(a=b);
System.out.println("After Swapping " + a + " " + b);
*/
}
}