C# Corner
Tech
News
Videos
Forums
Jobs
Books
Events
More
Interviews
Live
Learn
Training
Career
Members
Blogs
Challenges
Certification
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Refer
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Swap Two Variables Without Using a Third Variable in C#
WhatsApp
Syed Talib
6y
22
k
0
12
25
Blog
In most swap operations, we use a third variable. The following code snippet shows how we can use just two int variables to swap their values.
using
System;
namespace
swap
{
class
Program
{
static
void
Main(
string
[] args)
{
int
first, second;
Console.WriteLine(
"ENTER THE FIRST NO"
);
first =
int
.Parse(Console.ReadLine());
Console.WriteLine(
"ENTER THE Second NO"
);
second =
int
.Parse(Console.ReadLine());
if
(first != second)
{
first = first + second;
second = first - second;
first = first - second;
Console.WriteLine(
"The First No is After Swapping {0}"
, first);
Console.WriteLine(
"The Second No is After Swapping {0}"
, second);
Console.ReadLine();
}
else
Console.WriteLine(
"The First and Second No Should be Different"
);
Console.ReadLine();
}
}
}
C# SWAP VARIABLES
People also reading
Membership not found