Articles

How to swap two numbers without declaring third variable ?

C++ | 9/23/2020 8:35:57 AM


First of all you need to understand that swaping is possible or not without declaring third variable. 
Yes It is possible. You can exchange values of two variables without initialization of third variable.Let's start that how it is possible.

Example: 
using namespace std;
#include <conio.h>;
#include <iostream>;

int main() {
int x = 4; //initialization value of x int y = 5; //initialization value of y
x = x + y; y = x - y; x = x - y;
cout<<"Now value of 'X' is = \t"<<x<<endl; cout<<"Now value of 'Y' is = \t"<<y<<endl;
getch(); return 0;

}

  Output :   Now value of 'X' is =         5
            Now value of 'Y' is =         4