Hello Learner
Contributor
Stats
 Views : 26136 Comments : 0 Rating : 0
Actions

# swapping three variables without using temp/fourth variable in .NET

By Ashok Nalam on 12 Dec 2012 | Category: C# | Tagged: variable swap
In this snippet we will see how to swap three numbers without using fourth/temp variable
 Rate this resource

### Introduction

In 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 variable

`class 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
 Rate this resource