We can swap two numbers without using third variable. The below program is common ways to swap two numbers without using third variable
Let’s see a simple C# example to swap two numbers without using third variable.
using System; namespace OppsConcept { class Program { static void Main(string[] args) { int A = 5, B = 10; Console.WriteLine("Before Swap value of A: " + A); Console.WriteLine("Before Swap value of B: " + B); B = B - A; A = A + B; Console.WriteLine("After Swap value of A: " + A); Console.WriteLine("After Swap value of B: " + B); Console.ReadKey(); } } }
Before swapping the value of A & B
Before Swap value of A: 5 Before Swap value of B: 10
After swapping the value of A & B
After Swap value of A: 10 After Swap value of B: 5
Learn Recommended posts :
C# Program to Check a Number is Prime Number or not
C# Program to find sum of elements in a given array
C# program to find maximum and minimum element in array
How do you reverse a string in C#
C# program to Check String is palindrom or not
How to print Pyramid Pattern in C#
How do you count the no of digits or character in C#
Factorial Number Program in C# using Recursion
Convert string to char array in C#
Finding the biggest of three numbers in C#
C# Program to Check Whether the Entered Number is an Armstrong Number or Not