1) Swapping two numbers using a third variable.
using System;
namespace SwappingPrograms
{
public class Program
{
public static void Main()
{
int number1 = 10, number2 = 20, temp = 0;
Console.WriteLine($"Before SWapping number1= {number1}, number2 = {number2}");
temp = number1; //temp=10
number1 = number2; //number1=20
number2 = temp; //number2=10
Console.WriteLine($"After swapping number1= {number1}, number2 = {number2}");
Console.ReadLine();
}
}
}
2) Swap two integers without using the third variable.
using System; namespace SwappingPrograms { public class Program { public static void Main() { int number1 = 10, number2 = 20; Console.WriteLine($"Before SWapping number1= {number1}, number2 = {number2}"); number1 = number1 * number2; //number1=200 (10*20) number2 = number1 / number2; //number2=10 (200/20) number1 = number1 / number2; //number1=20 (200/10) Console.WriteLine($"After swapping number1= {number1}, number2 = {number2}"); Console.ReadLine(); } } }
Output :