In this example you will learn how to swap two numbers in java.
Java Code: Write a program to how to swap two numbers in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | public static void main(String[] args) { float first = 2.2f, second = 10.4f; 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:
In this example you have learned how to swap two numbers in java.