Java Examples
Java ExamplesPrograms with output

Java Program to swap two numbers without using third variable


Example:
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

Share this page on:

Was this page helpful ?

Let us know how we did!

Subscribe Email Updates

to get latest update