Swapping

 Swapping

Problem Description


Swap two number using bitwise


Note:

Use ^ (XOR) operator for this.operator

Logic Test Case 1


Input (stdin)

23


45


Expected Output


After Swap i=45 j=23

Logic Test Case 2


Input (stdin)

12


3


Expected Output


After Swap i=3 j=12





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 i=s.nextInt();

       int j=s.nextInt();

       i=i^j;

       j=i^j;

       i=i^j;

       System.out.print("After Swap");

       System.out.print(" i=" +i);

       System.out.println(" j=" +j);

}

}

Post a Comment

0 Comments