Loading...

Swapping of two numbers in C# without built in Method with Source Code


Swap two Numbers in C#:

It is an easy task to swap two numbers in C# without using any built in function. To swap two number first we take the two numbers that we store a number let say second number in another variable. Then we changes the value of second variable and assigned it the value of first variable. Now the value of second variable is not deleted as we already stored in another variable so we will assign its value to First number.

Code:

static void Main(string[] args)
        {
          
           int num1 = 10, num2 = 20;

           int remember_value = num2;  // storing value of second variable in another variable
           num2 = num1;  // assigning the first value to num2
           num1 = remember_value;  // va,ue of num2 is saved in remember_value
           Console.WriteLine("AFTER SWAPPING\n\n");
           Console.WriteLine("Num1 = {0}\n Num2 = {1}", num1, num2); // Displaying


        }

Output:



Hit Like Button if you understood, It helps a lot to keep us Motivated.

0 comments:

Post a Comment

Google+

 
TOP