``````// Program to show swap of two no’s without using third variable

#include<stdio.h>

#include<conio.h>

void main() {
int a, b;

printf("\nEnter value for num1 & num2 : ");
scanf("%d %d", &a, &b);

a=a+b-(a=b); //Swaping

printf("\nAfter swapping value of a : %d", a);
printf("\nAfter swapping value of b : %d", b);
getch();
}
``````

## All 13 Replies

Is there a question to this, or are you wanting to just post a code snippet? As I read it, your code to actually do the swap will not work. You will need to assign to both `a` and `b` to perform a swap, but you just assign to `a` twice.

This is how to do it:

``````#include <stdio.h>

int main(void)
{
int a = 10;
int b = 12;

a ^= b;
b ^= a;
a ^= b;
printf("a == %d, b == %d\n", a, b);
}
``````

that is a good code.

There are many ways to do it. You can use the XOR operations as ruberman has done it.

You can try one more method using addition and substraction:

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

It will also work as it is without third variable. Hope this helps.

``````    //All three logic's works for swapping two numbers

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

a ^= b ^= a ^= b;  //OR you can use this

//OR use below logic

a = a + b;
b = a - b;
a = a - b;
}
``````

"Number" is ambiguous. Let's throw a wrench into the mix:

``````double a, b;
``````

Beware, the answer isn't as simple as you think. :D

@James sir,

You catch it. I have also thought the same when he said "number". The solution I gave will work for the int surely but it might give wrong for the double. Am I right? Thanks.

The solution I gave will work for the int surely but it might give wrong for the double. Am I right?

You are correct, sir! And why is that? :)

Hehe. You calling me sir. :-p I can't be your sir in this generation. :)

I think double representation in binary is compilcated. When I will add them, it might truncate some digits in binary representation of the resultant answer. And, when I will again substract the number, that might now give me the exact number which I expect.

For 6.13425 , it might give 6.13426 or like that. I am not sure with my explanation, but now want you to make me correct if I am wrong sir!. :)

Nope, you pretty much nailed it. When swapping floating-point using arithmetic, there's a strong chance that rounding errors will muck up the original values. This problem doesn't exist with a temporary variable or integral types.

You can get around it by working with the bits directly (ie. the arithmetic approach is poor in this case), but then it becomes a question of whether the solution is complicated so much as to negate any savings of avoiding a temporary variable in the first place. And that's assuming there were any savings at all, taking compiler optimizations into consideration.

Another problem with the arithmetic approach is overflow and underflow, but that's another discussion. Ultimately I don't like that solution period. ;)

Long story short, swapping without a temporary variable is a neat trick, but only suitable for very niche and low level situations.

What deceptikon said. This where coding boffins start calculating the number of cycles each valid approach uses, and determine if there is a big enough difference to matter. If you are doing this a gazillion times a second, a few cycles may be important, at least for the initial target CPU architecture! The next one may behave very differently... See x86 vs Sparc vs ARM vs ...

`a=a+b-(a=b); //Swaping`
works for floats

commented: You need to assign to b to actually swap. -1

Nice 1..... Pls i need help here.

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.