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);

Output :


Sandeep Singh Shekhawat

Sandeep Singh Shekhawat is a Software Developer. He is awarded for C# Corner MVP(2013,2014 and 2015) and CodeProject MVP (2015). His three articles at CodeProject are prize wining in article competition. His more ... Read more