Swap two numbers without using temporary variable

Description:

Swap two numbers without using temporary variable

Code:

package com.techonol.algos;

public class SwapingTwoNumbers {

	public static void main(String a[]) {
		int x = 10;
		int y = 20;

		System.out.println("Before The 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 The Swap:");
		System.out.println("x value: " + x);
		System.out.println("y value: " + y);
	}
}

Output:

Before The Swap:
x value: 10
y value: 20
After The Swap:
x value: 20
y value: 10

 

<< Previous Program | Next Program >>

error: Content is protected !!