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

COMMENT USING
PREMIUM SPONSORS
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.
SPONSORED BY
  • MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.