+92 332 4229 857 99ProjectIdeas@Gmail.com

Swapping values without using third variable (C++)


Swapping values without using third variable
The following code shows you how two swap between two values without using third variable.

Code

#include "stdafx.h"
#include "iostream"
#include "conio.h"

using namespace std;

int main()
{
       int a;
       int b;

       cout<<"Enter A > ";
       cin>>a;
        
       cout<<"Enter B > ";
       cin>>b;

       a = a + b;
       b = a - b;
       a = a - b;
         
       cout<<"After Swapping A is > "<<a<<endl;
       cout<<"After Swapping B is > "<<b<<endl;
      
       _getche();
       return 0;
}

Output
Enter A > 12
Enter B > 21
After Swapping A is > 21
After Swapping B is > 12

0 comments: