Write a java program to swap two numbers without using third variable.
Java Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
public static void main(String[] args) { float first = 12.0f, second = 24.5f; System.out.println("--Before swap--"); System.out.println("First number = " + first); System.out.println("Second number = " + second); first = first - second; second = first + second; first = second - first; System.out.println("--After swap--"); System.out.println("First number = " + first); System.out.println("Second number = " + second); } |
Output: