From f6a2a67668dd3f19bfe846145c161cbebc6a5aa6 Mon Sep 17 00:00:00 2001 From: RishabhK88 <55580550+RishabhK88@users.noreply.github.com> Date: Sun, 13 Oct 2019 12:59:06 +0530 Subject: [PATCH] Swap without extra variable --- Swap without extra variable | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 Swap without extra variable diff --git a/Swap without extra variable b/Swap without extra variable new file mode 100644 index 0000000..eb29487 --- /dev/null +++ b/Swap without extra variable @@ -0,0 +1,15 @@ +// C++ Program to swap two numbers without +// using temporary variable +#include +using namespace std; + +int main() +{ + int x = 10, y = 5; + + // Code to swap 'x' and 'y' + x = x + y; // x now becomes 15 + y = x - y; // y becomes 10 + x = x - y; // x becomes 5 + cout << "After Swapping: x =" << x << ", y=" << y; +}