Friday, 17 June 2016

Program to swap two numbers without using Temporary Variable.

Hint:
Use Addition and Subtraction Operators.

Explanation :
If n=4,m=5 then output will be n=5,m=4.

Code:

public class Swap
{
       public static void main(String args[])
      {
         int x = 10;
         int y = 20;
         x = x+y;
         y = x-y;
         x = x-y;
        System.out.println("After swap:");
        System.out.println("x value: "+x);
        System.out.println("y value: "+y);
     }
}

No comments:

Post a Comment