Java : Program to swap two number without using any arithmetic operator


import java.util.Scanner;

/*
*
* Swap two number without using any arithmetic operator like + , - etc
 * we can achieve the solution using the XOR operator
*/


public class SwapTwoNumberWithoutUsingOperator {

public static void main(String[] args) {

Scanner sc = new Scanner(System.in);
System.out.println("Enter number A : ");
int a = sc.nextInt();
System.out.println("Enter number B : ");
int b = sc.nextInt();


System.out.println("Number before swapped A and B respectively --> " + a +" " + b);

a = a^b;
b = a^b;
a = a^b;

System.out.println("Number after swapped A and B respectively --> " + a +" " + b);


}

}

/* output
Enter number A :
10
Enter number B :
30
Number before swapped A and B respectively --> 10 30
Number after swapped A and B respectively --> 30 10

*/

No comments:

Post a Comment

Android : How to connect your Android device over Wifi using ADB command for App debugging

How to connect your Android device over Wi-Fi using ADB command Sometimes it requires to connect your Android dev...