Java program to swap two numbers using XOR operator

Java Programming Java8Object Oriented Programming

Following is a program to swap two numbers using XOR operator.

Example

public class ab31_SwapTwoNumberUsingXOR {
   public static void main(String args[]){
      int a,b;
      Scanner sc = new Scanner(System.in);
      System.out.println("Enter a value :");
      a = sc.nextInt();
      System.out.println("Enter b value :");
      b = sc.nextInt();
      a = a^b;
      b = a^b;
      a = a^b;
      System.out.println("Value of the variable a after swapping : "+a);
      System.out.println("Value of the variable b after swapping : "+b);
   }
}

Output

Enter a value :
55
Enter b value :
64
Value of the variable a after swapping : 64
Value of the variable b after swapping : 55
raja
Published on 26-Apr-2018 06:40:38
Advertisements