Swapping two variables without using third variable

 Swapping two variables without using third variable



Problem Description


Swapping two variables refers to mutually exchanging the values of the variables. Generally, this is done with the data in memory.

Logic Test Case 1


Input (stdin)

5 2


Expected Output


2 5

Logic Test Case 2


Input (stdin)

135 652


Expected Output


652 135



Code Area


import java.io.*;

import java.util.Scanner;

public class TestClass

  public static void main(String[] args)

  { 

  Scanner s=new Scanner(System.in);

  int x=s.nextInt();

  int y=s.nextInt();

  x = x+y;

  y = x-y;

  x = x-y;

  System.out.println(+x +" " +y);

}

}

Post a Comment

0 Comments