There are three ways to swap the number

Omniitstudent
Posted by Omniitstudent under C# category on | Points: 40 | Views : 1255
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication25
{
class Program
{
static void Main(string[] args)
{
//1st way
int nNum1 = 10;
int nNum2 = 20;
Console.WriteLine("Before swap");
Console.WriteLine(nNum1+"\n"+nNum2);

int temp = nNum1;
nNum1 = nNum2;
nNum2 = temp;
Console.WriteLine("after swap");
Console.WriteLine(nNum1 + "\n" + nNum2);

//2st way note-without taking third variable
int nNum3 = 10;
int nNum4 = 20;
Console.WriteLine("Before swap");
Console.WriteLine(nNum3 + "\n" + nNum4);
nNum3 = nNum3 + nNum4;
nNum4 = nNum3 - nNum4;
nNum3 = nNum3 - nNum4;
Console.WriteLine("after swap");
Console.WriteLine(nNum3 + "\n" + nNum4);
//3st way
int nNum5 = 10;
int nNum6 = 20;
Console.WriteLine("Before swap");
Console.WriteLine(nNum5 + "\n" + nNum6);
nNum5= nNum5 ^nNum6;
nNum6 = nNum5 ^ nNum6;
nNum5 = nNum5 ^ nNum6;
Console.WriteLine("after swap");
Console.WriteLine(nNum5 + "\n" + nNum6);
Console.ReadLine();
}
}
}

============================================
10
20
==
20
10
===========================================

Comments or Responses

Login to post response