Java Program to swap two numbers without using third variable


-

-

Example:

Copied
public class SwapingTwoNumber {
	public static void main(String a[]) {
		int x = 25;
		int y = 30;
		System.out.println("Before swap:");
		System.out.println("x value: "+x);
		System.out.println("y value: "+y);
		x = x+y;
		y=x-y;
		x=x-y;
		System.out.println("After swap:");
		System.out.println("x value: "+x);
		System.out.println("y value: "+y);
	}
}

Output:

Before swap:
x value: 25
y value: 30
After swap:
x value: 30
y value: 25