SpletIn this article, we will discuss how to swap two numbers without using a temp variable in C#. We can swap two numeric values (like int, float etc) without a temporary variable as follows : a = a + b ; b = a – b ; a = a – b ; We can also use XOR (^) operator for the same : a = a^b; b = b^a; a = a^b; This is a frequently asked interview question. Splet22. jul. 2024 · It was mentioned that this can be used to swap the values of the two variables. I tried it out as below: int a = 5, b = 10; Console.WriteLine(a + " " + b); // Prints --> …
Swap two numbers in C - TutorialsPoint
Splet06. sep. 2024 · As you can see in the below image, it’s very simple to swap two numbers with a third temporary variable. Program to swap two numbers by using third/temporary variable in C# is provided below: int num1, num2, temp; //Get two number from the user to swap. Console.WriteLine ("Enter first number"); num1 = int.Parse (Console.ReadLine ()); SpletIn this article, we will discuss how to swap two numbers without using a temp variable in C#. We can swap two numeric values (like int, float etc) without a temporary variable as … picture of chesapeake bay
Swap two numbers in C# – Csharp Star
SpletThe below algorithm is used to swap two numbers using XOR: first = first ^ second second = first ^ second first = first ^ second. Where first and second are the first and second … Splet16. feb. 2024 · 1) Take the input of the two numbers. 2) Store the sum of both the numbers in the first number and store the difference of both the numbers in the second number. 3) … Splet#KhalidTechSubscribe to our channel and hit the bell 🔔🔔🔔 icon to get video updates. Swap Two Numbers Without Temporary Variable In C Sharp Swap Two Numb... picture of chestnut tree