class SwapWithXOR
{
public static void Main()
{
int j, k;
Console.WriteLine("Enter two integers \n");
j= int.Parse(Console.ReadLine());
k = int.Parse(Console.ReadLine());
Console.WriteLine("\n Before swapping j= {0} and k = {1}", j, k);
j= j^ k;
k = j^ k;
j= j^ k;
Console.WriteLine("\n After swapping j= {0} and k = {1}", j, k);
Console.ReadLine();
}
}
------------------------
OUTPUT
Enter two integers
30
40
Before swapping j= 30 and k = 40
After swapping j= 40 and k = 30
------------------------
{
public static void Main()
{
int j, k;
Console.WriteLine("Enter two integers \n");
j= int.Parse(Console.ReadLine());
k = int.Parse(Console.ReadLine());
Console.WriteLine("\n Before swapping j= {0} and k = {1}", j, k);
j= j^ k;
k = j^ k;
j= j^ k;
Console.WriteLine("\n After swapping j= {0} and k = {1}", j, k);
Console.ReadLine();
}
}
------------------------
OUTPUT
Enter two integers
30
40
Before swapping j= 30 and k = 40
After swapping j= 40 and k = 30
------------------------