How To Swap Two Varaible without third Varaible In C#

Hello Guys! Welcome to my Blog!

HOW TO SWAP TWO VARAIBLE WITHOUT THIRD VARAIBLE IN C#?

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

namespace swap
{
    class Program
    {
   
     
        static void Main(string[] args)
        {
            int a = 100;
            int b = 200;
            Console.WriteLine("BEFORE SWAP:" + a + "," + b);
            a = a + b;//a =300(100+200)
            b = a - b;//b=100(300-200)
            a = a - b;b=200(300-100)
            Console.WriteLine("\n\n\nAFTER SWAP:" + a + "," + b);
            Console.ReadLine();
        }
    }
}

OUTPUT!

BEFORE SWAP : 100,200
AFTER SWAP:200,100

Comments

Post a Comment