Friday 16 October 2015

Swaping using two variables














This program is used to swap two numbers without using temporary variable.



using System;

namespace Swaping
{
    class Program
    {
        static void swap(ref int a,ref int b)
        {
            a = a + b;
            b = a - b;
            a = a - b;
        }
        static void Main(string[] args)
        {
            int i = 10;
            int j = 33;
            Console.WriteLine("Before swapping:i={0},j={1}",i,j);
            swap(ref i,ref j);
            Console.WriteLine("after swapping:i={0},j={1}",i,j);
        }
    }
}

2 comments:

  1. Thats an interesting Post...
    Check out more programs related to c,c++,C# and many more at www.cfetch.blogspot.com

    ReplyDelete