9
Reply

Swap two numbers without using any third variable and arithmetic operator.

Ahmar Husain

Ahmar Husain

10y
8.8k
0
Reply

    int a=10; int b=2; a=a^b; b=a^b; a=a^b;

    Not get

    int a =10;int b=20;a = a + b; /// a=30,b=20b = a - b; /// b=10,a=30a = a - b; /// a=20,b=10

    i=10; j=20; i = i ^ k; k = i ^ k; i = i ^ k;

    i=10; j=20; i = i ^ k; k = i ^ k; i = i ^ k;

    int a =10,int b=20;a= a b;b= a-b;a= a-b;

    x = x * y; y = x / y; x = x / y;

    using System;namespace SwapTwoNoWithoutUsing3rdVarArithOperator {class Program{static void Main(string[] args){int i = 65; int k = 120; Console.WriteLine("The value of i={0} and j={1} before swapping" ,i,k);i = i ^ k; k = i ^ k; i = i ^ k;Console.WriteLine("The value of i={0} and j={1} after swapping", i, k);Console.ReadLine();}} }

    The solution is two use XOR operator
                int Fisrt = 221;
                int Second = 100;
                Fisrt = Fisrt ^ Second;
                Second = Fisrt ^ Second;
                Fisrt = Fisrt ^ Second;
                Console.WriteLine(Fisrt);
                Console.WriteLine(Second);
                Console.ReadLine();