wap to swap two numbers without using third variable in java code example

Example 1: swapping with two variables in java

public class Exercise15 {
  public static void main(String[] args) {
     // int, double, float
   int a, b;
   a = 15;
   b = 27;
   System.out.println("Before swapping : a, b = "+a+", "+ + b);
   a = a + b;
   b = a - b;
   a = a - b;
   System.out.println("After swapping : a, b = "+a+", "+ + b);
 }
 
}

Example 2: java program to swap two numbers

int a, b, c;
a = 5;
b = 3;
System.out.println("Before SWAP a = " + a + ", b = " + b);
c = a;
a = b;
b = c;
System.out.println("After SWAP a = " + a + ", b = " + b);

Tags:

Java Example