Results 1 to 8 of 8

Thread: How to swap two numbers without using a third temporary variable?

  1. #1
    Join Date
    Aug 2011
    Posts
    28

    How to swap two numbers without using a third temporary variable?

    Hey hi all, I would like, to know some suggestion on my problem. The question is that how can I swap two numbers without using a third temporary variable? I aam waiting for the response, if you have any answer about tit then reply me back.

  2. #2
    Join Date
    May 2009
    Posts
    527

    Re: How to swap two numbers without using a third temporary variable?

    I think that yes, this is possible to swap two numbers without using a third temporary variable. For doing this thing you might be need to take extra thread on it.

  3. #3
    Join Date
    Apr 2009
    Posts
    488

    Re: How to swap two numbers without using a third temporary variable?

    Ok, assume that there are two numbers like x and y then:

    x = x+y
    y=x-y
    x=x-y.

  4. #4
    Join Date
    May 2009
    Posts
    539

    Re: How to swap two numbers without using a third temporary variable?

    sorry friend, but I am getting confused with your question, will you don’t have b any book or some thing else, I think that you have created a thread on this topic.

  5. #5
    Join Date
    Mar 2010
    Posts
    154

    Re: How to swap two numbers without using a third temporary variable?

    According to me, the code for the swapping two number without using the first number is given as follows:
    a= a^b;
    b = b^a;
    a = a^b;

  6. #6
    Join Date
    May 2009
    Posts
    529

    Re: How to swap two numbers without using a third temporary variable?

    In this case I will ask you for the operating system which you are using for your query. The above reply will working this case if you are using the C or C++, but also in this case try to not use one line XOR technique, because this thing is undefined and not portable.

    If it's Python:
    v1, v2 = v2, v1

  7. #7
    Join Date
    May 2009
    Posts
    511

    Re: How to swap two numbers without using a third temporary variable?

    In your case you can also take the help of the pointers. Pointers are the variable which stores the memory location of the variable. With the help of the pointers I am sure that you are able to swap the variable without using the third variable.

  8. #8
    Join Date
    May 2009
    Posts
    637

    Re: How to swap two numbers without using a third temporary variable?

    Hey in this case, I still think that it will require another variable, unless the address of two variables is consecutive. I am telling this thing because as soon as you will apply swap on pointer variable at this time the previous value will be lost and also you cannot swap both the variable in the single operation. So for the swapping I will suggest you to follow the approach that nims11 provided. Hope this will help you.

Similar Threads

  1. Need C program code to count numbers of negative & positive numbers
    By Sarfaraj Khan in forum Software Development
    Replies: 5
    Last Post: 16-01-2010, 02:00 PM
  2. What is the program to swap two numbers?
    By Bharat89 in forum Software Development
    Replies: 3
    Last Post: 26-11-2009, 09:37 AM
  3. Interchange 2 numbers without extra variable in C
    By KAMAL60 in forum Software Development
    Replies: 3
    Last Post: 03-11-2009, 06:37 PM
  4. Replies: 2
    Last Post: 28-08-2009, 07:51 PM
  5. SQL Query for Searching Missing Numbers from Sequence of Numbers
    By Bhagwandas in forum Software Development
    Replies: 3
    Last Post: 18-02-2009, 01:47 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,975,622.36950 seconds with 16 queries