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

🌫️ Project Title: "Fog Buster – AI-Powered Visibility Enhancement System"

🔍 Project Vision: To design a device that allows vehicles (cars, trucks) and aircraft (planes, helicopters, drones) to see clearly during f...