Created
July 6, 2016 19:23
-
-
Save sanjeevshrestha/8df9a0e248ca94b223437bae207f8f35 to your computer and use it in GitHub Desktop.
Swapping two numbers without third variable in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static void swap1(int a, int b) { | |
System.out.println("The intital value " + a + " " + b); | |
a = a + b; | |
b = a - b; | |
a = a - b; | |
System.out.println("The swapped value " + a + " " + b); | |
} | |
public static void swap2(int a, int b) { | |
System.out.println("The intital value " + a + " " + b); | |
a = a * b; | |
b = a / b; | |
a = a / b; | |
System.out.println("The swapped value " + a + " " + b); | |
} | |
public static void swap3(int a, int b) { | |
System.out.println("The intital value " + a + " " + b); | |
a = a ^ b; | |
b = a ^ b; | |
a = a ^ b; | |
System.out.println("The swapped value " + a + " " + b); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment