In one of the previous tutorial, I have explained how to swap two numbers in java programming language. In this tutorial, I will share how to write a java program to swap number without a temporary variable.
Copy the below java program and execute it with the help Java compiler. At the end of this tutorial, I have shared the output of the following programs.
public static void main(String args)
int x, y;
System.out.println("Enter the number x and y:");
Scanner in = new Scanner(System.in);
x = in.nextInt();
y = in.nextInt();
System.out.println("Before Swapping:\nx = "+x+"\ny = "+y);
x = x + y;
y = x - y;
x = x - y;
System.out.println("After Swapping:\nx = "+x+"\ny = "+y);
Enter the number x and y:
If you like FreeWebMentor and you would like to contribute, you can write an article and mail your article to [email protected] Your article will appear on the FreeWebMentor main page and help other developers.