If you looking to save memory and don't want to use third variable you can follow the below approach to swap two numbers. Please note that it is tricky interview question also.
using System;
namespace MyApp
{
class my_class
{
static void Main(string[] args)
{
int intOne = 1;
int intTwo = 2;
intOne = intOne + intTwo;
intTwo = intOne - intTwo;
intOne = intOne - intTwo;
Console.WriteLine("intOne : {0}", intOne);
Console.WriteLine("intTwo : {0}", intTwo);
Console.ReadLine();
}
}
}
No comments:
Post a Comment