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

C# Program to Swap two numbers without 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;
 Console.WriteLine("C# Program to Swap two numbers without 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);
 
 iFirstNumber = iFirstNumber+iSecondNumber;
 iSecondNumber = iFirstNumber-iSecondNumber;
 iFirstNumber = iFirstNumber -iSecondNumber;
 Console.WriteLine("Number after swapping");
 Console.WriteLine("First Number = {0}", iFirstNumber);
 Console.WriteLine("Second Number = {0}", iSecondNumber);
 Console.ReadKey();

 }
 }
}


Output:

Swap two numbers without using third varibale

View More:

Conclusion:

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

One Thought to “C# Program to Swap two numbers without using third variable”

  1. Wonderful goods from you, man. I’ve understand your stuff previous to and you are just too excellent. I really like what you’ve acquired here, certainly like what you are stating and the way in which you say it. You make it enjoyable and you still care for to keep it sensible. I cant wait to read far more from you. This is actually a tremendous web site.

     

Leave a Comment