as the title suggest i like to swap two numbers but the thing is that i should not use 3rd variable that is doing the whole stuff only with 2 variables so plz any body help me out

Are you reading How to Program for the 1970's?

Googling will get you the answer you are seeking, but then after you know it, don't ever use it for programming today or in the future -- use a temporary variable. Solving yesterday's problems today is useful to understand the techniques, but the solution may no longer be applicable to current systems.

By the way, "XOR swap" might be a good phrase for your search.

Swapping two variables is an easy stuff if you are using temporary variable

void swap(int& a,int& b)
{
     int temp=a;
               a=b;
               b=temp;
}
 
 
//in C++ template
template<typename _Type> 
void swap(_Type& t, _Type& t2)
{
   _Type temp=t;
           t=t2;
           t2=temp;
}

using only the two variables requires a little maths.
check that out it might work for ints.

void swap(int& a,int& b)
{
    a=a+b;
    b=a-b;
    a=a-b;
}

Hope you got the basic idea........... try it out.....
The XOR algorithm is another choice and probably good one ...........and its simple check that out urself. remember (A ^ B) ^ B=A :)

using only the two variables requires a little maths.
check that out it might work for ints.

void swap(int& a,int& b)
{
    a=a+b;
    b=a-b;
    a=a-b;
}

Hope you got the basic idea........... try it out.....
The XOR algorithm is another choice and probably good one ...........and its simple check that out urself. remember (A ^ B) ^ B=A :)

The more important part is knowing when the addition and subtraction fails for integers, and how the XOR swap may fail, and how to correctly code the XOR swap. And also why this doesn't work for some other variable types.

But as I said, after you've learned these techniques, remember never[*] to use them.

[*]Never say never.

The more important part is knowing when the addition and subtraction fails for integers, and how the XOR swap may fail, and how to correctly code the XOR swap. And also why this doesn't work for some other variable types.

But as I said, after you've learned these techniques, remember never[*] to use them.

[*]Never say never.

ya thaxs it works

ya thaxs it works

Except where it doesn't. That was what I was trying to get you to think about.

> ya thaxs it works
Rather too easy to break IMO

#include <stdio.h>

int swap ( int *a, int *b ) {
  *a ^= *b;
  *b ^= *a;
  *a ^= *b;
}
int main(void)
{
  int a = 2, b = 3;
  printf( "%d %d\n", a, b );
  swap( &a, &b );
  printf( "%d %d\n", a, b );
  swap( &a, &a ); /* swap with itself */
  printf( "%d %d\n", a, b );
  return 0;
}

What do you get with the final line of output?
Is it what you expected?

hi
Swapping without using the 3rd variable can be done by using

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

here a & b are the 2 numbers u want to swap.:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.