Showing posts with label Swap two values with out using third variable.. Show all posts
Showing posts with label Swap two values with out using third variable.. Show all posts

Sunday, September 3, 2017

Swap two values with out using third variable.

Swap two values with out using third variable.



using System; 
class swap
{
           
       public static void Main(string[] args)
       {
         int a,b;
         Console.WriteLine("Enter the value for a");
         a=Convert.ToInt32(Console.ReadLine());

         Console.WriteLine("Enter the value for b");
          b=Convert.ToInt32(Console.ReadLine());
         
         Console.WriteLine("Before swaping:-");
         Console.WriteLine("a=" +a);
         Console.WriteLine("b="+b);
          a=a+b;

          b=a-b;
          a=a-b;
         Console.WriteLine("After swaping:-");
         Console.WriteLine("a=" +a);
         Console.WriteLine("b="+b);
         Console.Read();

       }

}

output

before:-
a=1
b=2
After:-
a=2
b=1