How to Swap of two number without using 3rd or temp variable?

Total Post:11

Points:55
C# 
C#
 1125  View(s)
Ratings:
Rate this:
  1. Post:11

    Points:55
    How to Swap of two number without using 3rd or temp variable?

    It is a program to swap two number without using the 3rd variable.

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace ConsoleApplication4
    {
        class Program
        {
            static void Main(string[] args)
                int num1, num2;
                string y;
                do
                {
                   Console.Write("Enter the First Number : ");
                    num1 = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter the Second Number : ");
                    num2 = Convert.ToInt32(Console.ReadLine());
                    num1 = num1 + num2;
                    num2 = num1 - num2;
                    num1 = num1 - num2;
                    Console.WriteLine("Swapping is completed \n 1st number is {0} \n 2nd number is {1}", num1, num2);
                    Console.WriteLine("Do you want to Repeat Y/N \n");
                    y = Console.ReadLine();
                    Console.ReadLine();
                      } while (y == "y" || y == "Y");
                if (y == "n" || y == "N")
                Console.WriteLine("Exiting the program");
                else
                Console.WriteLine("You have entered wrong entry exiting the program");
                Console.ReadLine();
                }
                }
        }

      Modified On Mar-28-2018 06:47:20 AM
  1. Post:70

    Points:350
    How to Swap of two number without using 3rd or temp variable?

    It is very useful program for swapping of two number.


    I have also another code for the same 

     Console.Writeline("Enter the First Number : ");
                    num1 = Convert.ToInt32(Console.ReadLine());
                    Console.Write("Enter the Second Number : ");
                    num2 = Convert.ToInt32(Console.ReadLine());
                    num2=num1*num2/(num1=num2);
                    Console.WriteLine(" number1 : {0} number2 : {1}", num1, num2);\


Answer