C# C# Console

Swap Numbers Without Using Temporary Variables in C#

In this example, i’ll show you how to swap two numbers without using temporary variables in C#.

C# Code:

 

  1. Initially, a = 5 and b = 10.
  2. Then, we add a and b and store it in a with the code a = a + b. This means a = 5 + 10. So, a = 15 now.
  3. Then we use the code b = a - b. This means b = 15 - 10. So, b = 5 now.
  4. Again, we use the code a = a - b. This means a = 15 - 5. So finally, a = 10.

Hence, the numbers have been swapped.

 

Output:

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.