Monday, April 30, 2018

Swap three variables without using fourth variable !!

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

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

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...