Menu Bar

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Thursday, 10 November 2016

Java Program to Swap Two Variables Without Using Third Variable?

Previously we Have Discussed the Program for
Java Program to find Common Elements from two Arrays ?


Below Example shows how to swap two variables without using third variable. The Simple logic behind swapping is to add and subtract both values in given variables.

public class SwapTwoNumbersWithoutThirdVariable {

 public static void main(String[] args) {
 int a = 12;
 int b = 6;
 
 System.out.println("BEFORE SWAP : A = "+a+" ,B = "+b);
 a = a+b;
 b = a-b;
 a = a-b;
 System.out.println("AFTER  SWAP : A = "+a+" ,B = "+b);
}

}
Program Output:

BEFORE SWAP : A = 12 ,B = 6
AFTER  SWAP : A = 6 ,B = 12



      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

1 comment: