C# Program to swap two numbers without third variable

We can swap two numbers without using third variable. The below program is common ways to swap two numbers without using third variable

Let’s see a simple C# example to swap two numbers without using third variable.

using System;
namespace OppsConcept
{
     class Program
     {
        static void Main(string[] args)
        {
            int A = 5, B = 10;
            Console.WriteLine("Before Swap value of A: " + A);
            Console.WriteLine("Before Swap value of B: " + B);

            B = B - A;
            A = A + B;

            Console.WriteLine("After Swap value of A: " + A);
            Console.WriteLine("After Swap value of B: " + B);

            Console.ReadKey();

        }
     }
}

Before swapping the value of A & B

Before Swap value of A: 5
Before Swap value of B: 10

After swapping the value of A & B

After Swap value of A: 10
After Swap value of B: 5

C# Program to swap two numbers without third variable

Learn Recommended posts :

Leave a Reply

Your email address will not be published. Required fields are marked *