Monday, April 30, 2018

Swap two variables without using third variable !!

  1. public class SwapTwoNumberWithoutThirdVariable {
  2. public static void main(String[] args) {
  3. int a = 10;
  4. int b = 20;

  5. a = a + b;
  6. b = a - b;
  7. a = a - b;
  8. System.out.println("after swap value of a: " + a);
  9. System.out.println("after swap value of b: " + b);
  10. }
  11. }
Output:
after swap value of a: 20
after swap value of b: 10

No comments:

Post a Comment

Blueprint for self-improvement

To learn faster: Make the process fun To understand yourself : Write To understand the world better : Read To build deeper connection : Lis...