WebHosting

Showing posts with label swapping. Show all posts
Showing posts with label swapping. Show all posts

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;
}

Featured Posts

How to Auto-Terminate Frozen and Hung Apps in Windows

We have all been there. You are in the middle of an important project or a gaming session, and suddenly, your screen freezes. The dreaded ...