IntroductionIn some of interviews, interviewers will ask write a program to swap three numbers without using a fourth variable or temporary (temp) variable. So in this snippet we will see how to swap three numbers without using fourth/temp variable.
Program: Swapping 3 numbers Without temp variableclass SwapThreeNumbersWithoutUsingFourthVariable { public static void Main() { Console.WriteLine("/****** Program to swap 3 numbers without using temporary variable *******/"); int a = 10, b = 20,c=30; Console.WriteLine(" Values before swapping a={0} , b={1} , c={2}", a, b,c); a = a + b + c; b = a - (b + c); c = a - (b + c); a = a - (b + c); Console.WriteLine(" Values after swapping a={0} , b={1}, c={2}", a, b,c); Console.ReadKey(); } } Output:Values before swapping a=10 , b=20 , c=30 Values after swapping a=30 , b=10, c=20
|