C# Program to Swap Numbers Using Xor Operator

C# Program to Swap Numbers Using Xor Operator Article has many meanings. We are going to know this topic clearly through this article. We hope you like this article.

C# Program to Swap Numbers Using Xor Operator

C# Program to Swap Numbers Using Xor Operator

using System;
using System.Text;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            int a = 0;
            int b = 0;
            
            //reading numbers
            Console.Write("Enter first number: ");
            a = int.Parse(Console.ReadLine());
            Console.Write("Enter second number: ");
            b = int.Parse(Console.ReadLine());

            //printing the numbers before swapping
            Console.WriteLine("Before swapping...");
            Console.WriteLine("a = {0} \t b = {1}", a, b);

            //swapping 
            a = a ^ b;
            b = a ^ b;
            a = a ^ b;

            //printing the numbers after swapping
            Console.WriteLine("After swapping...");
            Console.WriteLine("a = {0} \t b = {1}", a, b);

            //hit ENTER to exit
            Console.ReadLine();
        }
    }
}
Enter first number: 100
Enter second number: 200
Before swapping...
a = 100          b = 200
After swapping...
a = 200          b = 100

Final Words

C# Program to Swap Numbers Using Xor Operator We got to know our topic very clearly. And if you have any doubts about this article you can let us know your doubts via comment.

Hi, I'm Selva a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Deekshi. Here, I post about programming to help developers.

Share on:

Leave a Comment