Swap Two Numbers Without Using Third Variable in Java

Swap Two Numbers Without Using Third Variable

In this program, you will learn to Swap two numbers without a third variable, Which usually involves using mathematical operations to switch the values of two variables. For example, one could use the following equation to switch the values of two variables, x and y:

x = x + y;

y = x y;

x = x y;

Program:

Output:

C:\CodeRevise\java>javac SwapTwoNumbers2.java

C:\CodeRevise\java>java SwapTwoNumbers2

Enter the value of a and b  4 8

Before Swap: a= 4 b= 8

After Swap: a= 8 b= 4

 

 

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top