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

How to run standalone mock server on local laptop

 Please download the standalone wiremock server from Direct download section at the bottom of the page.  Download and installation Feel fre...