DotNet Mirror
  DNM facebook   DNM Google+   DNM Twitter   

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
  Discuss Add Comment   |  
Add rating 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
  Discuss Add Comment    
Add rating Rate this resource   
About the Contributor
Member Since : 10 Dec 2012
Member Points (Level) : 9226  (Professional)
Location : INDIA
Home Page : http://dotnetmirror.com
About : I am admin of this site.
Rate this resource
 
Add your Comment
Name Email WebSite
Captcha Refresh


Comments (0)
No comments found, click here to add comment.
 
cheap jordans|wholesale air max|wholesale jordans|wholesale jewelry|wholesale jerseys