Swap Two Integer Number Without Third Variable

By Sandeep Singh Shekhawat 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

COMMENT USING

PREMIUM SPONSORS

Hire Mobile & Web Developer on demand. 100% satisfaction. Try for 1 week or Money Back. Local and remote developers available all over USA.

Latest Blogs