HOW TO SWAP OF TWO NUMBER WITHOUT USING 3RD OR TEMP VARIABLE?

Ailsa Singh

Total Post:11

Points:55
Posted by  Ailsa Singh
C# 
C#
 876  View(s)
Ratings:
Rate this:
  1. Ailsa Singh

    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 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 Sep-14-2016 01:18:43 AM
  1. Abhishek Srivasatava

    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

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!