Interviews10 answers
Swap two numbers without using any third variable and arithmetic operator.
10 Answers
int a=1;
int b=2;
a=a b;
b=a-b;
a= a-b;
console.writeline(a.tostring());
console.writeline(b.tostring());
console.readline();
Result :
2
1
kamar rahiPosted Apr 13, 2018, 8:31 AM EDT
Not get
Aman RohillaPosted Mar 21, 2017, 8:09 PM EDT
int a=10;
int b=2;
a=a^b;
b=a^b;
a=a^b;
anusha rajuPosted Aug 18, 2015, 5:39 AM EDT
int a =10;int b=20;
a = a + b; /// a=30,b=20
b = a - b; /// b=10,a=30
a = a - b; /// a=20,b=10
Devinder KumarPosted Jul 8, 2015, 2:38 AM EDT
i=10;
j=20;
i = i ^ k;
k = i ^ k;
i = i ^ k;
Rahul PrajapatPosted Jun 1, 2015, 6:10 PM EDT
i=10;
j=20;
i = i ^ k;
k = i ^ k;
i = i ^ k;
Pankaj Kumar ChoudharyPosted Jun 1, 2015, 8:58 AM EDT
int a =10,int b=20;
a= a b;
b= a-b;
a= a-b;
satish kumarPosted May 23, 2015, 10:25 AM EDT
x = x * y;
y = x / y;
x = x / y;
Sanjay SinghPosted May 2, 2014, 4:59 AM EDT
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();
}
}
}
Praveen SinhaPosted May 1, 2014, 11:17 AM EDT
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();
int Fisrt = 221;
int Second = 100;
Fisrt = Fisrt ^ Second;
Second = Fisrt ^ Second;
Fisrt = Fisrt ^ Second;
Console.WriteLine(Fisrt);
Console.WriteLine(Second);
Console.ReadLine();
Ahmar HusainPosted Nov 25, 2013, 1:49 AM EST





