Swapping of two numbers program in C# without using 3rd variable.

A simple program of swapping of two number without using 3rd variable in C#.

Source code - Swapping of two numbers program in Console(C#).


    using System;
    using System.Collections.Generic;
    using System.Text;

    namespace swapping_numbers
    {
        class Program
        {
            static void Main(string[] args)
            {
                int first, second;
                Console.WriteLine("ENTER FIRST NUMBER :- ");
                first = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("ENTER SECOND NUMBER :- ");
                second = Convert.ToInt32(Console.ReadLine());
                first = first + second;
                second = first - second;
                first = first - second;
                Console.WriteLine("VALUES AFTER SWAPPING :- {0} {1} ",first,second);
                Console.ReadLine();
            }
        }
    }	
	

Output

	
   ENTER FIRST NUMBER :- 5
   ENTER SECOND NUMBER :- 2
   
   VALUES AFTER SWAPPING :- 2 5
           
Advertisement here

Share this Page.