By: Prem Tiwari | Last Updated:
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:
Prem Tiwari is the founder of FreeWebMentor.com and also a professional developer who has vast experience in PHP and open source technologies. Apart from this, he is a blogger by hobby and also he has been a regular speaker of WordPress sessions in various IT Companies. View all posts by Prem Tiwari