To SWAP two numbers C#

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace value
{
    class SWAP
    {
        static void Main(string[] args)
        {
            int a;
            int b;
            int c;//refrence variable

            Console.WriteLine("To SWAP the two numbers");
            Console.WriteLine("enter the first number");
            a= int.Parse( Console.ReadLine());
            Console.WriteLine("enter the second number");
            b = int.Parse(Console.ReadLine());
            Console.WriteLine("two number are {0} {1}",a,b);

            //SWAP Logic
            c = a;
            a = b;
            b = c;

            Console.WriteLine("Swaped number are {0}  {1}", a,b);         
            Console.ReadLine();
        }
    }
}

output:-













----------------------------------------------------

OR

using System;

namespace Test_Application
{
    class Program
    {
        static void Main(string[] args)
        {
            string  x,y,z;

            Console.WriteLine("First letter");
             x = Console.ReadLine();
            Console.WriteLine("Second letter");
             y = Console.ReadLine();

            z = x;
            x = y;
            y = z;

            Console.WriteLine("{0}{1}", x, y);
            Console.Read();
         
        }
    }
}

Share this

Previous
Next Post »