Swapping of two number without using 3rd variable in c#
If I have two values assigned to two seperate variables and I want to exchange or swap the values one another without using the third variable. We can do above task easily with a use of 3rd variable, but without using is tedious task. How to swap two number without using 3rd variable in c#
Learn Swapping of two number without using 3rd variable in c#
Let us do a small console application for this task.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace ConsoleApplication3
{
class Program
{
static void Main(string[] args)
{
int first = 12, second = 20;
Console.WriteLine("first value::::" + first);
Console.WriteLine("second value::::" + second + "\n\n");
first = first + second;
second = first - second;
first = first - second;
Console.WriteLine("first value::::" + first);
Console.WriteLine("second value::::" + second);
Console.Read();
}
}
}
From the above code what I am doing is getting the First and Second string from user Input. In the first step I assigned the sum of first and second variable values to First variable. Next subtract first variable and second variable and assigned to Second variable. Third step is subtract first variable and second variable and assigned to first variable.
Good one. Thanks for sharing it.
The below code will work as well and it's a one line code.