-->




Program to swap two variables without using third or temp variable

9 comments
Program Logic Steps:
  • Store sum of both in var1.
  • Subtract var2 from var1 and store it in var2 (now var2 has value of var1) .
  •     Subtract var2 from var1 and store it in var1 (now var1 has    value of var2).
  •  
Image View of Code

C++ code to swap two variables without using third variable:

#include<iostream>

using namespace std;

int main()

{

int var1, var2;

 cout<<"Enter value for first integer:  ";

 cin>>var1;

 cout<<"Enter value for second integer:  ";

 cin>>var2;

 cout<<" Values Before swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

              var1=var1+var2;

              var2=var1-var2;

              var1=var1-var2;

 cout<<" Values After swapping:  "<<endl;

 cout<<"First Integer ="<<var1<<endl;

 cout<<"Second Interger ="<<var2<<endl;

    return 0;

}



Sample Output



 Image view of code:

9 comments:

  1. Interesting exercise. Just to make sure people aren't led astray, I'd like to point out why it should never, ever be done in a "real" program:

    1. It will only work with reasonably small integers. If you get an overflow (i.e., var1+var2 >= 2^31), the result is undefined.
    2. It won't work for float or double, because floating point addition and subtraction is not perfectly accurate.
    3. It won't work for other types.
    4. It is hard to read.
    5. It is inefficient.

    Instead:

    using std::swap;

    swap(var1, var2);

    ReplyDelete
    Replies
    1. You Are Absolutely Right
      But This Program IS For Absolute Beginners That Is Why Program Coded In Very Simple Format With Simple Logic So Anyone Can Get The Basic Idea Of Swapping.

      Your Points Are Valid For Advanced Level.

      Thanks A Lot.. For Giving Your Time.

      Delete
  2. I was going to suggest the xor trick (for integer types), because it doesn't have the overflow problem, but after some thought and reading of the spec, I realized:

    1. For unsigned integer types, overflow is impossible (because the C++ standard mandates that unsigned arithmetic be done modulo 2**n), so both the addition-subtraction method, and the xor method, will work.

    2. For signed types, with some representations it might be possible for the xor method to create invalid values (for example, in signed magnitude or 1s complement representations, depending on how positive and negative zeros are treated) resulting in undefined behavior. With 2s complement representation, both the addition-subtraction method and the xor method will probably work (because the easiest way to implement 2s complement arithmetic is to have it wrap around).

    Anyway, anonymous is right, std::swap is always the right answer!

    ReplyDelete
  3. I want a program to search an element in an array and replace every occurrence with 0.
    Example: Input is 1232472. The places where 2 is present must be made 0 so that we will get 1030470 and it must be swapped as 1347000. How to write a program for that?

    ReplyDelete
    Replies
    1. #include
      using namespace std;
      void main(void)
      {
      int x, i,j;
      int arr[] = { 1, 2, 3, 2, 4, 7, 2 };
      for (i = 0; i < 7; i++)
      {
      if (arr[i] == 2) arr[i] = 0;
      }
      for (i = 0; i < 7; i++)
      cout << arr[i];
      cout << endl;
      for (i = 0; i < 7; i++)
      {
      if (arr[i] == 0)
      {
      for (j = i; j < 7; j++){
      if (arr[j] =! 0){
      arr[i] = arr[j];
      arr[j] = 0;
      break;

      }
      }

      }
      }
      for (i = 0; i < 7; i++)

      cout << arr[i];
      system("pause");
      }
      its not working but you get the idea!!!!!!!!!!!!

      Delete
  4. A very good article. Thanks a lot

    ReplyDelete
  5. Thanks a lot fr d effort sir can i hav more n more exercise on basis of data structure ?

    ReplyDelete