It was November 2014, my second week at the university where I was studying electrical engineering. In the first semester, one of my courses was **Introduction to Programming** and we were being taught the **C++** programming language. It was my first encounter with the world of programming because, before this, I had studied biology in high school. After a brief introduction to `cin`

, `cout`

& variables, our instructor gave us a test.

We were supposed to write a program that had two `int`

variables, `a`

& `b`

and the task was to swap their values without using a third variable. Of course, at that point I didn't have any introduction to pointers or this would've been a walk in the park.

Anyway, I remember quite vividly that as soon as I read the statement of the question, I somehow knew to not start coding right away. I don't know why but I naturally reached for a pen and a piece of paper and started doing math. I started by assigning random integer values to `a`

and `b`

and the plan was to use the arithmetic operations to come up with some sort of solution that will be able to perform the swapping. So I wrote on the paper:

```
a = 7
b = 5
=> a + b = 12
=> a - b = 2
```

Now with just `+`

and `-`

, I had two more numbers i.e. `12`

and `2`

. I thought of using the new numbers to somehow retain the information about the original values so that I could reverse-engineer them later. What if we assign the value of `a + b`

to `a`

? We are gonna lose the original value of `a`

but let's see what happens:

```
a = a + b
=> a = 12
=> b = 5
```

Although, I've reassigned `a`

to be `a + b`

but notice how I still have access to the original value of `a`

and I can easily get it by doing `a - b`

. And this is exactly what we need to store in `b`

so let's do that:

```
b = a - b
=> b = 12 - 5
=> b = 7
```

So now `b`

has the original value of `a`

but the original value of `b`

is gone. Or is it? What if we do:

```
a = a - b
=> a = 12 - 7
=> a = 5
```

**Ha, problem solved! 🤩**

I am sure I had to go through a little more hit and trial than I explained here but eventually, it worked.

Then I quickly turned towards my laptop, and wrote the algorithm as shown below:

```
#include <iostream>
int main() {
int a = 7;
int b = 5;
std::cout << "Before: a=" << a << ", b=" << b << "\n";
a += b;
b = a - b;
a -= b;
std::cout << "After: a=" << a << ", b=" << b << "\n";
return 0;
}
```

And ran it in the terminal:

```
$ g++ swap.cpp && ./a.out
Before: a=7, b=5
After: a=5, b=7
```

Sure enough, it worked perfectly. Then as a sanity check, I tested a couple more use cases with negative values for `a`

and `b`

and it still worked.

I don't mean to brag but I was the only one in the whole class who solved this problem that day 🤣

## Conclusion

So you might ask, why am I writing about this now?

Well, so many times we just dive into solving problems without fully understanding what the problem is. And I am guilty of this as well. But whenever I make this mistake, it always brings me back to this event where I just naturally knew that I should solve this problem on a piece of paper first to gain some perspective and then write code. This approach is especially useful when you have a numerical problem at hand. I hope you can also relate to this and it can remind you in the future to always understand the problem first, build enough context, divide it into simple steps and then start writing code. 🤓