Swap Two Variables Without Using a Third Variable in C#

This code sample shows how we can swap two integers without using a third integer.

In most  swap operations, we use a third variable. The following code snippet shows how we can use just two int variables to swap their values.
  1. using System;  
  2. namespace swap  
  3. {  
  4.     class Program  
  5.     {  
  6.         static void Main(string[] args)  
  7.         {  
  8.             int first, second;  
  9.             Console.WriteLine("ENTER THE FIRST NO");  
  10.             first = int.Parse(Console.ReadLine());  
  11.             Console.WriteLine("ENTER THE Second NO");  
  12.             second = int.Parse(Console.ReadLine());  
  13.             if (first != second)  
  14.             {  
  15.                 first = first + second;  
  16.                 second = first - second;  
  17.                 first = first - second;  
  18.                 Console.WriteLine("The First No is After Swapping   {0}", first);  
  19.                 Console.WriteLine("The Second No is After Swapping {0}", second);  
  20.                 Console.ReadLine();  
  21.             }  
  22.             else  
  23.                 Console.WriteLine("The First and Second No Should be Different");  
  24.             Console.ReadLine();  
  25.         }  
  26.     }  
  27. }