Monday 19 September 2011

Swap Numbers Without Using Third Variable Java Example

Swap Numbers Without Using Third Variable Java Example

  1. /*
  2. Swap Numbers Without Using Third Variable Java Example
  3. This Swap Numbers Java Example shows how to
  4. swap value of two numbers without using third variable using java.
  5. */
  6. public class SwapElementsWithoutThirdVariableExample {
  7. public static void main(String[] args) {
  8. int num1 = 10;
  9. int num2 = 20;
  10. System.out.println("Before Swapping");
  11. System.out.println("Value of num1 is :" + num1);
  12. System.out.println("Value of num2 is :" +num2);
  13. //add both the numbers and assign it to first
  14. num1 = num1 + num2;
  15. num2 = num1 - num2;
  16. num1 = num1 - num2;
  17. System.out.println("Before Swapping");
  18. System.out.println("Value of num1 is :" + num1);
  19. System.out.println("Value of num2 is :" +num2);
  20. }
  21. }
  22. /*
  23. Output of Swap Numbers Without Using Third Variable example would be
  24. Before Swapping
  25. Value of num1 is :10
  26. Value of num2 is :20
  27. Before Swapping
  28. Value of num1 is :20
  29. Value of num2 is :10
  30. */

No comments:

Post a Comment

Complete Details about eLitmus pH Test at Rs:699/- Share your java material and fresher interview Information for us to Help Others... mail to : vhkrishnan.v@gmail.com