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

πŸ””Announcement : Visit the new website/blog πŸ””

πŸ””πŸ””πŸ”” Announcement :  Visit the new website/blog πŸ””πŸ””πŸ”” Dear readers, Thank you for reading my blog. If you could have used the little info...