Swap two numbers without using third variable in Java

public class SwapTwoNumbersExample {

	public static void swapTwoNumbers(int number1, int number2) {
		System.out.println("Before Swap  number1:"+number1 + " number2:" + number2);
		number1 = number1 + number2;
		number2 = number1 - number2;
		number1 = number1 - number2;
		System.out.println("After  Swap  number1:"+number1 + " number2:" + number2);
		System.out.println();
	}

	public static void main(String[] args) {
		swapTwoNumbers(23, 45);
		swapTwoNumbers(67, 203);
		swapTwoNumbers(156, 4);
	}
}


Output
Before Swap number1:23 number2:45 After Swap number1:45 number2:23 Before Swap number1:67 number2:203 After Swap number1:203 number2:67 Before Swap number1:156 number2:4 After Swap number1:4 number2:156










Related Examples


Write a program to get list of all the files from a given directory?
Write a Comparator to compare students based upon their name and age?
Simple Array Sorting Program.
Bubble Sort Example.
Write a java program to check if a number is Armstrong number or not?








Receive Latest Java Examples in Your Email: