How to swap two numbers without using a third variable in C#

By Focusoncode November 23, 2018

When we are asked to swap two numbers, we would use a third variable that would help us do that. But what if we were asked to swap them without using a third variable?

Below you will find a solution for that in C#, but it can easily be used also in other programming languages.

The code in C#:

The result:

 

As you can see in order  to swap the numbers we are gone use:

 

I have used try and catch in case the user hasn’t typed a number but a letter or something else.

Leave a Comment

Your email address will not be published.