C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » General »

how to swap two numbers without using a third variable


Posted Date: 09 Jan 2009      Posted By: karthe      Member Level: Silver     Points: 1   Responses: 8



hi

how to swap two numbers without using a third variable.
eg: a=5 b=10
it should be swaped as a=10 b=5

thanks in advance
karthe





Responses

Author: Pascal    10 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Here is the logic from my side.

a = b - a;
b = b - a;
a = a + b;

Console.WriteLine(a + ", " + b);



Author: karthe    10 Jan 2009Member Level: SilverRating: 2 out of 52 out of 5     Points: 1

hi pascal,

thanks for ur reply.
but its not working for few numbers.
eg: a=13, b=7

could u help me

regards
karthe



Author: Gaurav Agrawal    10 Jan 2009Member Level: DiamondRating: 2 out of 52 out of 5     Points: 2

its working properly
to verify use calculator with minus sign
i think its a very good logic and if in ur application or coding this has been failed then can u tell us in which language and what cade u r using????

GA

Thanks & Regards,

Gaurav Agrawal
Sr.Software Engineer
gaur1982@yahoo.com
09829373514



Author: Pascal    10 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

Hey,

Its working for me.. Here is the complete code.
Copy this in Main function
---------------------------
Console.WriteLine("Enter first number :");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number :");
int b = Convert.ToInt32(Console.ReadLine());
SwapNumbers(a, b);
Console.ReadLine();

Copy this outside main
----------------------
private static void SwapNumbers(int a, int b)
{
Console.WriteLine("After Swapping: ");
a = b - a;
b = b - a;
a = a + b;
Console.WriteLine(a + ", " + b);
}



Author: Sonia sardana    10 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

b=b-a
a=b+a
b=a-b

EX--Suppose a=18 b=65
b=65-18=47
a=47+18=65
b=65-47=18


UR Example--Suppose a=5 b=10
b=10-5=5
a=5+5=10
b=10-5=5

Reply if u got it.



Author: Sonia sardana    10 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 1

ki karthe check mine reply its even working for numbers a=13, b=7
b=7-13=-6
a=-6+13=7
b=7-(-6)=13



Author: Dhaval Shah    16 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 4

Its working for me.. Here is the complete code.
Copy this in Main function
---------------------------
Console.WriteLine("Enter first number :");
int a = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("Enter second number :");
int b = Convert.ToInt32(Console.ReadLine());
SwapNumbers(a, b);
Console.ReadLine();

Copy this outside main
----------------------
private static void SwapNumbers(int a, int b)
{
Console.WriteLine("After Swapping: ");
a = b - a;
b = b - a;
a = a + b;
Console.WriteLine(a + ", " + b);
}


Example :

b=b-a
a=b+a
b=a-b

EX--Suppose a=18 b=65
b=65-18=47
a=47+18=65
b=65-47=18


UR Example--Suppose a=5 b=10
b=10-5=5
a=5+5=10
b=10-5=5

Reply if u got it.



Author: Julian    30 Jan 2009Member Level: GoldRating: 2 out of 52 out of 5     Points: 2

Hi,

The problem is when you minus the one number.

e.g. a = 7, b = 5.

a = b - a, then you will get a negative number.

So, just check which is the larger number and use that number to subtract and the solution will work :]....



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : application not working in mozilla and google chrome..
Previous : Diff btn Flow Chart and Data Flow Diagram?
Return to Discussion Forum
Post New Message
Category: General

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use