1public class Exercise15 {
2 public static void main(String[] args) {
3 // int, double, float
4 int a, b;
5 a = 15;
6 b = 27;
7 System.out.println("Before swapping : a, b = "+a+", "+ + b);
8 a = a + b;
9 b = a - b;
10 a = a - b;
11 System.out.println("After swapping : a, b = "+a+", "+ + b);
12 }
13
14}
15
16
1public class Main{public static void main(String[] args) {float a = 3.66f, b = 6.47f;System.out.println("a number " + a);System.out.println("b number " + b);float temp = a;a = b;b = temp;System.out.println("After swapping");System.out.println("a number " + a);System.out.println("b number " + b);}}
1// swap three variables in java without using temporary variable
2public class SwapThreeNumbersWithoutTemp
3{
4 static int num1, num2, num3;
5 public static void main(String[] args)
6 {
7 num1 = 30; num2 = 60; num3 = 90;
8 System.out.println("Before swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
9 swapWithoutTemporary();
10 System.out.println("After swapping three numbers: num1 = " + num1 + ", num2 = " + num2 + ", num3 = " + num3);
11 }
12 static void swapWithoutTemporary()
13 {
14 num1 = num1 + num2 + num3;
15 num2 = num1 - (num2 + num3);
16 num3 = num1 - (num2 + num3);
17 num1 = num1 - (num2 + num3);
18 }
19}