Swap Numbers Without Using Third Variable Java Example /*         Swap Numbers Without Using Third Variable Java Example ...

Swap Numbers Without Using Third Variable Java Example

02:34 PASSOVE INCOME 0 Comments

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.                
  9.                 int num1 = 10;
  10.                 int num2 = 20;
  11.                
  12.                 System.out.println("Before Swapping");
  13.                 System.out.println("Value of num1 is :" + num1);
  14.                 System.out.println("Value of num2 is :" +num2);
  15.                
  16.                 //add both the numbers and assign it to first
  17.                 num1 = num1 + num2;
  18.                 num2 = num1 - num2;
  19.                 num1 = num1 - num2;
  20.                
  21.                 System.out.println("Before Swapping");
  22.                 System.out.println("Value of num1 is :" + num1);
  23.                 System.out.println("Value of num2 is :" +num2);
  24.         }
  25. }
  26. /*
  27. Output of Swap Numbers Without Using Third Variable example would be
  28. Before Swapping
  29. Value of num1 is :10
  30. Value of num2 is :20
  31. Before Swapping
  32. Value of num1 is :20
  33. Value of num2 is :10
  34. */

0 comments: