Today I am going to write a C# program to Swap two numbers using third variable.

C# Program to Swap two numbers using third variable:

Create a Console Application Project and write the following code.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SwapNumbers
{
class Program
{
static void Main(string[] args)
{

int iFirstNumber, iSecondNumber, iTemp;
Console.WriteLine("C# Program to Swap two numbers using third variable");
Console.WriteLine("Enter the First Number");
iFirstNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter the Second Number");
iSecondNumber = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Number before swapping");
Console.WriteLine("First Number = {0}", iFirstNumber);
Console.WriteLine("Second Number = {0}", iSecondNumber);
iTemp = iSecondNumber;
iSecondNumber = iFirstNumber;
iFirstNumber = iTemp;
Console.WriteLine("Number after swapping");
Console.WriteLine("First Number = {0}", iFirstNumber);
Console.WriteLine("Second Number = {0}", iSecondNumber);
Console.ReadKey();
}
}
}

Output:

Swap two numbers using third variable

View More:

C# Program to Sort String Array in Descending Order.

C# Program to Sort a String Array in Ascending Order.

How to Save to and Retrieve Image from Database in C# Windows Application.

Conclusion:

Hope you understand how to swap two number using the third variable

 

 

Leave a Comment