Java Code To Check Swap Two Numbers [Without Temp Variable]

March 7, 2023

Program:

import java.util.*;

public class SwapWithout 
{
	public static void main(String[] args) 
	{
		int x = 100;
		int y = 200;
		x = x + y;
		y = x - y;
		x = x - y;
		System.out.println("X=" + x + " and Y=" + y);
	}
}

Compile:

javac SwapWithout.java
java SwapWithout

Output:

X=200 and Y=100

You Might Also Like