C++ Program To Swap Two Numbers Without Using Third Variable

#include <iostream>
using namespace std;
int main()
{
    int a,b ;
    cout<<"Enter 1st number :: ";
    cin>>a;
    cout<<"\nEnter 2nd number :: ";
    cin>>b;
    cout << "\nBefore swapping, Numbers are :: " << endl;
    cout << "\n\ta = " << a << ", b = " << b << endl;
    a = a + b;
    b = a - b;
    a = a - b;
    cout << "\nAfter swapping, Numbers are :: " << endl;
    cout << "\n\ta = " << a << ", b = " << b << endl;
    return 0;
}

OUTPUT:

Enter 1st number :: 5
Enter 2nd number :: 9
Before swapping, Numbers are ::
        a = 5, b = 9
After swapping, Numbers are ::
        a = 9, b = 5

Leave a Comment