Sponsored Ad

Saturday, March 12, 2011

How to Interchange Values of Two Variables Without using Third Variable using C#

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.

How to Interchange Values of Two Variables Without using Third Variable using C#

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

Sponsored Ad

Development Updates