WebHosting

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

How to Enable and Configure Remote Desktop (RDP) on Ubuntu 24.04.4 LTS

Remote Desktop Protocol (RDP) support in Ubuntu has become considerably easier with the GNOME Remote Desktop stack included in modern Ubuntu...