Thursday, March 22, 2018

Swap Value of Two Variable Without Using 3rd Variable


The below code swaps two given values of variables without using the third variable in C#.


<!DOCTYPE html>
 <head></head>
 <body>
  <form>
   <div>
   <h3>Swap Value of Two Variable Without Using 3rd Variable</h3>
   <p><u>Value before swaping </u></p>
   Value of A :<asp:Label ID="lblA" runat="server"></asp:Label><br />
   Value of B :<asp:Label ID="lblB" runat="server"></asp:Label><br />
   <p><u>Value after swaping </u></p>
   Value of A :<asp:Label ID="lblAS" runat="server"></asp:Label><br />
   Value of B :<asp:Label ID="lblBS" runat="server"></asp:Label><br />
   </div>
  </form>
 </body>
 </html>

C# Code for swapping value of variable :

protected void Page_Load(object sender, EventArgs e)
 {
  int a = 10, b=20;
  lblA.Text = a.ToString();
  lblB.Text = b.ToString();
  a = a + b;
  b = a - b;
  a = a - b;
  lblAS.Text = a.ToString();
  lblBS.Text = b.ToString();
}

No comments:

Post a Comment