-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSwapTwoNumberExample.java
More file actions
45 lines (42 loc) · 1.46 KB
/
SwapTwoNumberExample.java
File metadata and controls
45 lines (42 loc) · 1.46 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 com.codinginterview;
public class SwapTwoNumberExample {
public static void main(String[] args) {
System.out.println("Swap Two Number Example");
int a = 10;
int b = 30;
System.out.println("The Number a is before swap:: "+ a);
System.out.println("The Number b is is before swap:: "+ b);
a = a + b;
b = a - b;
a = a - b;
System.out.println("The Number a is after swap:: "+ a);
System.out.println("The Number b is after swap:: "+ b);
}
private void swapUsingThridVar(int a, int b){
System.out.println("Before Swap Values ::"+ a + " , "+ b);
int temp;
temp = a;
a = b;
b = temp;
System.out.println("After Swap Values ::"+ a + " , "+ b);
}
private void swapUsingMultDiv(int a , int b){
System.out.println("Before Swap Values ::"+ a + " , "+ b);
a = a*b;
b = a/b;
a = a/b;
System.out.println("After Swap Values ::"+ a + " , "+ b);
}
private void swapUsingBitwiseOp(int a , int b){
System.out.println("Before Swap Values ::"+ a + " , "+ b);
a = a^b;
b = a^b;
a = a^b;
System.out.println("After Swap Values ::"+ a + " , "+ b);
}
private void swapUsingOtherLogic(int a , int b){
System.out.println("Before Swap Values ::"+ a + " , "+ b);
a = (a + b) - (a=b);
System.out.println("After Swap Values ::"+ a + " , "+ b);
}
}