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
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
5y
21.9
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
Up Next
Swap Two Numbers Without using Third Variable in C#
Ebook Download
View all
Programming Strings using C#
Read by 23.4k people
Download Now!
Learn
View all
Membership not found