Swap two number without third variable in C++ by R4R Team

What is Swapping:

Example-
Two variable are a=10,b=20
After Swapping:
a=20,b=10

program:

#include< iostream>
using namespace std;
int main()
{
int a,b,temp;
cout<<"Enter two numbersn";
cin>>a>>b;
cout<<"Before swapping:n";
cout<<"a and b is: "<< a<<" "<< b<< endl;
a=a+b;
b=a-b;
a=a-b;
cout<<"After swapping:n";
cout<<"a and b is: "<< a<<" "<< b;
return 0;
}


output-

Enter two numbers
10 20
Before swapping
a and b is: 10 20
After Swapping
a and b is: 20 10




Leave a Comment:
Search
Categories
R4R Team
R4Rin Top Tutorials are Core Java,Hibernate ,Spring,Sturts.The content on R4R.in website is done by expert team not only with the help of books but along with the strong professional knowledge in all context like coding,designing, marketing,etc!