Swap Two Numbers without Third Variable in Java

Swap Two Numbers without Third Variable in Java

Here you will learn to Swap Two Numbers without Third Variable in Java, 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 to Swap Two Numbers without Third Variable in Java

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

 

 

Check out our other Java Programming Examples

 

 

 

Leave a Comment

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

Scroll to Top