Articles

C++ program to swap two variable without using third variable

In this chapter of C++ program tutorial out task is:

• Write a C++ program to swap two variables without using third variable.

C++ program to swap two variable without using third variable

Given below  are the three different methods to swap two variable without using the third variable in c++

Method 1 :

``````#include<iostream>
using namespace std;
int main()
{
int a,b,temp=0;
cout<<"Enter the two variable"<<endl;
cin>>a>>b;
cout<<"Value before swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
a = a + b;
b = a - b;
a = a - b;
cout<<"Value after swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
return 0;
}
``````

OUTPUT :

Enter the two variable

10 20
Value before swapping
a = 10  b = 20
Value after swapping
a = 20  b = 10

EXPLANATION :
In the above code :
• The execution starts from the main function (here int main()).
• Then a 4 bit each for the two integers are created (assuming the system to be 64 bit).
• Then the compiler accepts the two variable as the input.
• The total value of "a" and "b" is stored in "a" by addition.
• Then in "b" the value of orginal "b" is subtracted form "a".
• Then in "a" the value of  "a" is subtracted form the new "b" and the value get reversed.
• The program than gives the desired output and terminates in the line "return 0" returning a 0 value.

WORKING:

a = 10 and b = 20

a = a + b     ( a = 10 + 20 = 30 )

b = a - b      ( b = 30 - 20 = 10 )

a = a - b      ( a = 30 - 10 = 20 )

So, a = 20 and b = 10

You can see the above code execution and output in codeblocks IDE:

Method 2:

``````#include<iostream>
using namespace std;
int main()
{
int a,b,temp=0;
cout<<"Enter the two variable"<<endl;
cin>>a>>b;
cout<<"Value before swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
a = a * b;
b = a / b;
a = a / b;
cout<<"Value after swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
return 0;
}
``````
OUTPUT :
Enter the two variable
10 20
Value before swapping
a = 10  b = 20
Value after swapping
a = 20  b = 10

EXPLANATION :
In the above code :
• The execution starts from the main function (here int main()).
• Then a 4 bit each for the two integers are created (assuming the system to be 64 bit).
• Then the compiler accepts the two variable as the input.
• The total value of "a" and "b" is stored in "a" by multiplication.
• Then in "b" the value of orginal "b" is divided form "a".
• Then in "a" the value of  "a" is divided form the new "b" and the value get reversed.
• The program than gives the desired output and terminates in the line "return 0" returning a 0 value.

WORKING:

a = 10 and b = 20

a = a * b     ( a = 10 * 20 = 200 )

b = a / b      ( b = 200 / 20 = 10 )

a = a / b      ( a = 200 / 10 = 20 )

So, a = 20 and b = 10

You can see the above code execution and output in codeblocks IDE:

Method 3: (Using Bitwise XOR)

``````#include<iostream>
using namespace std;
int main()
{
int a,b,temp=0;
cout<<"Enter the two variable"<<endl;
cin>>a>>b;
cout<<"Value before swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
a = a ^ b;
b = a ^ b;
a = a ^ b;
cout<<"Value after swapping"<<endl;
cout<<"a = "<<a<<"\t"<<"b = "<<b<<endl;
return 0;
}
``````
OUTPUT :
Enter the two variable
10 20
Value before swapping
a = 10  b = 20
Value after swapping
a = 20  b = 10

EXPLANATION :
In the above code :
• The execution starts from the main function (here int main()).
• Then a 4 bit each for the two integers are created (assuming the system to be 64 bit).
• Then the compiler accepts the two variable as the input.
• In the method a bitwise operator (XOR) is used.
• In the variable "a" a bitwise operation (XOR) of "a" and "b".
• In the variable "b" a bitwise operation (XOR) of changed value of "a" and "b".
• In the variable "a" a bitwise operation (XOR) of "a" and  changed value of "b".
• The program than gives the desired output and terminates in the line "return 0" returning a 0 value.

XOR TRUTH TABLE

X   Y     X^Y

0    0     0

1    0     1

0     1    1

1     1     0

WORKING:

a = 10 (01010) and b = 20 (10100)

a = a ^ b     ( a = 01010 ^ 10100 = 11110 [30] )

b = a ^ b      ( b = 11110 ^ 10100 = 01010 [10] )

a = a ^ b      ( a = 11110 ^ 01010 = 10100 [20] )

So, a = 20 and b = 10

You can see the above code execution and output in codeblocks IDE:

CPP Programs

Would you like to see your article here on tutorialsinhand. Join Write4Us program by tutorialsinhand.com