Step 1: Design Page – SwapNumbers.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="SwapNumbers.aspx.cs" Inherits="SwapNumbers" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Swap Two Numbers Without Third Variable - Real Time Example</title>
<style>
body {
font-family: Arial;
background-color: #f0f2f5;
margin: 50px;
}
.container {
width: 400px;
margin: auto;
background: white;
border-radius: 8px;
box-shadow: 0px 0px 10px #ccc;
padding: 20px;
}
h2 {
color: #1A2A80;
text-align: center;
}
.form-control {
width: 100%;
padding: 8px;
margin-top: 10px;
}
.btn {
background-color: #7A85C1;
color: white;
border: none;
padding: 10px 20px;
margin-top: 10px;
border-radius: 5px;
cursor: pointer;
}
.result {
font-weight: bold;
color: #333;
margin-top: 15px;
text-align: center;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div class="container">
<h2>Swap Two Numbers (Without Using Third Variable)</h2>
<asp:Label ID="lblFirst" runat="server" Text="Enter First Number:"></asp:Label><br />
<asp:TextBox ID="txtFirst" runat="server" CssClass="form-control"></asp:TextBox><br />
<asp:Label ID="lblSecond" runat="server" Text="Enter Second Number:"></asp:Label><br />
<asp:TextBox ID="txtSecond" runat="server" CssClass="form-control"></asp:TextBox><br />
<asp:Button ID="btnSwap" runat="server" Text="Swap Numbers" CssClass="btn" OnClick="btnSwap_Click" /><br />
<asp:Label ID="lblResult" runat="server" CssClass="result"></asp:Label>
</div>
</form>
</body>
</html>
Step 2: Backend Logic – SwapNumbers.aspx.cs
using System;
public partial class SwapNumbers : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSwap_Click(object sender, EventArgs e)
{
int num1, num2;
bool valid1 = int.TryParse(txtFirst.Text.Trim(), out num1);
bool valid2 = int.TryParse(txtSecond.Text.Trim(), out num2);
if (!valid1 || !valid2)
{
lblResult.Text = "Please enter valid numbers.";
lblResult.ForeColor = System.Drawing.Color.Red;
return;
}
// Swapping logic without using a third variable
num1 = num1 + num2; // Step 1: Add both numbers
num2 = num1 - num2; // Step 2: Subtract second from sum to get first
num1 = num1 - num2; // Step 3: Subtract new second from sum to get second
lblResult.Text = $"After swapping:<br/>First Number = {num1}<br/>Second Number = {num2}";
lblResult.ForeColor = System.Drawing.Color.Green;
}
}
Real-Time Example Flow
Open the page SwapNumbers.aspx.
Enter values — e.g., num1 = 10, num2 = 20.
Click “Swap Numbers”.
The output will display:
After swapping:
First Number = 20
Second Number = 10
Explanation
| Step | Description |
|---|
| Step 1 | num1 = num1 + num2 adds both numbers. |
| Step 2 | num2 = num1 - num2 gives the original value of num1. |
| Step 3 | num1 = num1 - num2 gives the original value of num2. |
| Result | Both numbers are swapped without using a third variable. |
Sample Input / Output
| Input | Output |
|---|
| 10, 20 | 20, 10 |
| 45, 100 | 100, 45 |
| 5, 8 | 8, 5 |