How to swap two numbers without using third variable

How to swap two numbers without using the third variable? If you are attending an interview for one or two year experience, 80% of the time you will face this question.

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest

If you are attending an interview for one or two year experience, 80% of the time you will face this question. “How to swap two numbers without using the third variable”?

I remember the days when I gave an interview as fresher. This was among the commonly asked interview question asked in almost all of the interviews. Though it’s a simple question. Still, people get confused when to plus and minus.

Here we go with the c# program or snippet of code for Swapping two numbers without using the third variable.

using System;

namespace Swap
{
    class Program
    {
        private static void Main(string[] args)
        {
            int a = 1000;
            int b = 999;
            Console.WriteLine("Before Swap");
            Console.WriteLine("Value of A :{0} ", a);
            Console.WriteLine("Value of B :{0} ", b);
            a = a + b;
            b = a - b;
            a = a - b;
            Console.WriteLine("After Swap");
            Console.WriteLine("Value of A :{0} ", a);
            Console.WriteLine("Value of B :{0} ", b);
            Console.ReadLine();
        }

    }
}

 

Do you think this article was useful? Feel free to provide your comment and concerns. If you need any help regarding any kind of interview questions and answers, any tutorial. let us know in the comment section. We will try our best to help you.

 

Share on facebook
Share on twitter
Share on linkedin
Share on pinterest
Share on whatsapp
Share on email
Author Bio

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Recommended for you
Connect With Us
Stay Connected

Subscribe to our newsletter today to receive updates on latest tech news, tutorials SEO tips many more.

Latest Articles
18518157_223940698108354_7866972342651071979_o
Apps

5 Best Apps for self-development

We live in a world of high dynamicity, progress, and constant change. But in most times it has been observed that we are not that welcoming when it comes to changing ourselves or changing anything around us. We try to repel change as long as possible.

Free hub for guest blogging nerds and newbie writers

  • Submit Guest Post
  • Share your Knowledge
  • Build your Backlinks
  • Advertise your products
  • Make money by writing
Subscribe to our newsletter today to receive updates on latest tech news, tutorials SEO tips many more.