Sunday, February 27, 2011

Several ways of swapping two variables with using temp variable and without using, in several line or in one line

 SWAPING........

#include <iostream>
using namespace std;
int main()
{
int a=10,b=20;
cout <<a<<"   "<<b<<endl;
a+=b-=a=b-a;
cout <<a<<"   "<<b<<endl;
a/=b=(a=a*b)/b;
cout <<a<<"   "<<b<<endl;
a^=b^=a^=b;
cout <<a<<"   "<<b<<endl;
a=(b=(a=b^a)^b)^a;
cout <<a<<"   "<<b<<endl;
b=a+b-(a=b);
cout <<a<<"   "<<b<<endl;
a=a^b;
b=a^b;
a=a^b;
cout <<a<<"   "<<b<<endl;
a=a+b;
b=a-b;
a=a-b;
cout <<a<<"   "<<b<<endl;
int temp=a;
a=b;
b=temp;
cout <<a<<"   "<<b<<endl;

return 0;
}

No comments:

Post a Comment

Thank you for Commenting Will reply soon ......

Featured Posts

#Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc

 #Linux Commands Unveiled: #date, #uname, #hostname, #hostid, #arch, #nproc Linux is an open-source operating system that is loved by millio...