Monday, April 30, 2018

Swap four variables without using fifth variables !!

Problem statement: Swap four variables without using fifth variables !!

Problem solving: 
  1. public class SwapFourNumberWithoutFifthVariable {
  2. public static void main(String[] args) {
  3. int a = 10;
  4. int b = 20;
  5. int c = 30;
  6. int d = 40;

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

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