Blog

Swap Two Integer Number Without Third Variable

Posted by Sandeep Singh Shekhawat Blogs | Visual C# Dec 02, 2012
Explains how to swap two numbers without using third variable.

Problem:  Create a program to swap to integer number without using third variable.

Process:  Addition of these two numbers and addition store in first number. Subtraction  of first number to second number.

Solution :

using System;
namespace SwapNumber
{
    class Program
    {
        static void Main(string[] args)
        {
            int firtsNumber = 10, secondNumber = 20;
            Console.WriteLine("First Number = {0} and Second Number = {1}", firtsNumber, secondNumber);
            firtsNumber += secondNumber;
            secondNumber = firtsNumber - secondNumber;
            firtsNumber = firtsNumber - secondNumber;
            Console.WriteLine("First Number = {0} and Second Number = {1}", firtsNumber, secondNumber);
            Console.ReadLine();
        }
    }
}

Output :

swap.PNG

post comment
     
COMMENT USING
PREMIUM SPONSORS
Over-C is a holistic consortium of communications and technology specialists. We build, deploy and market both business as well as consumer products and solutions.
SPONSORED BY
  • PDF reports have never been easier to create. With our included WYSIWYG Designer, you can layout your reports, set up your data source and let DynamicPDF ReportWriter do the rest.
Join a Chapter