You are here: Home / Topics / Swapping of Variables without using Third Variable in Java

Swapping of Variables without using Third Variable in Java

Filed under: Java on 2023-08-11 19:16:47

// Swapping example without third variable

class SwapWithoutThirdVariable
{
public static void main( String args[ ] )
{
 int   a, b;

 a = 10;
 b = 20;

 System.out.print("\nBefore Swapping : " );
 System.out.print( a + "   " + b );

 a = a + b;
 b = a - b;
 a = a - b;
 
 System.out.print( "\nAfter Swapping  : " );
 System.out.print( a + "   " + b );
}
}


Output:

Before Swapping :  10 20

After Swapping  : 20 10

About Author:
V
Vinay Kumar     View Profile
Hi, I am using MCQ Buddy. I love to share content on this website.