-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swapping.java
51 lines (38 loc) · 1.29 KB
/
Swapping.java
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
49
50
51
package programs;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class Swapping {
public static void SwapUsingTwoVariables() throws IOException
{
int a,b;
System.out.println(" ---------- Swap Two numbers --------- ");
System.out.print("Enter the first number a = ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
a = Integer.parseInt(s1);
System.out.print("Enter the second number b = ");
String s2 = br.readLine();
b = Integer.parseInt(s2);
b = a + b;
a = b - a;
b = b - a;
System.out.println("After swapping a = " + a + " and b = "+ b);
}
public static void SwapUsingThreeVariables() throws IOException
{
int a,b,temp;
System.out.println(" ---------- Swap Two numbers using Three variables --------- ");
System.out.print("Enter the first number a = ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s1 = br.readLine();
a = Integer.parseInt(s1);
System.out.print("Enter the second number b = ");
String s2 = br.readLine();
b = Integer.parseInt(s2);
temp = a;
a = b;
b = a;
System.out.println("After swapping a = " + a + " and b = "+ b);
}
}