Java Program to Swap Two Numbers Without Using Third Variable

Write a Write a Java program to swap two numbers without using third variable..



Source Code
class SwappingWithoutThirdVar {

	public static void main(String[] args) {
		
		int a = 75, b=45;
		
		System.out.println("Before swapping : a = "+a+" b = "+b);
		
		b = a + b;
		a = b - a;
		b = b - a;
		
		System.out.println("After swapping : a = "+a+" b = "+b);
	}
}
Output
C:\>javac SwappingWithoutThirdVar.java
C:\>java SwappingWithoutThirdVar
Before swapping : a = 75 b = 45
After swapping : a = 45 b = 75