# How will u swap three variable without using temp variable !??

Matt Swaggi
Ranch Hand
Posts: 61
hai friendz can any1 help me out !!!
is that possible to swap three variable without using a temp variable

Jesper de Jong
Java Cowboy
Saloon Keeper
Posts: 15482
43
This has been discussed before, see for example these posts:

swap without using temp variable
Swapping two float values with no temp varaible

Note: These are neat tricks, but you should not ever use such tricks in any serious piece of software.

Matt Swaggi
Ranch Hand
Posts: 61
please look in to my question one more time sir !
i have asked to swap THREE variable without using temp variable !

Ulf Dittmer
Rancher
Posts: 42968
73
Now that you know the way to swap two variables, maybe you extend the principle to three variables if you think about it?

Matt Swaggi
Ranch Hand
Posts: 61
Ulf Dittmer i dont know my friend ! help me out i cant think !
i know the trick by using addition, sub , only for two variables .
three variable i wonder ???

Henry Wong
author
Marshal
Posts: 21496
84
Originally posted by GUNA RANJAN:
Ulf Dittmer i dont know my friend ! help me out i cant think !
i know the trick by using addition, sub , only for two variables .
three variable i wonder ???

Think about it. Can you swap three variables? If you already have a way to switch two variables?

Maybe you can swap those three variables -- two at a time?

Henry

Matt Swaggi
Ranch Hand
Posts: 61
any practical example hendry ! ??

Charles Lyons
Author
Ranch Hand
Posts: 836
Maybe you can swap those three variables -- two at a time?
More than enough of a clue there. Since this sounds like a homework problem, I'm not going to tell you the answer.

However, suffice to say that using pairwise swaps you can achieve any number of permutations without using a "temp variable".

Note these techniques use (possibly more) space in the registers or CPU cache, so you really save nothing except 4 bytes of RAM by using them.

Campbell Ritchie
Sheriff
Posts: 50189
79
Originally posted by GUNA RANJAN:
hai friendz can any1 help me out !!!
I hadn't noticed that earlier. Use Real Words. You are not on a mobile, and not everybody here learned English as a first language; any1 and friendz are simply annoying.

Matt Swaggi
Ranch Hand
Posts: 61
Campbell Ritchie please see you private message

Matt Swaggi
Ranch Hand
Posts: 61
Thank You all the way which i worked is that correct ! if not denote my mistakes !

Swapping three variable without using temp variable

a = 2 b = 1 c = 3

a = a+b+c = 6

b = a -b-c = 2
c = a -b- c = 1
a = a-b-c = 3

After swaping

a = 3 ; b = 2 ; c = 1

Campbell Ritchie
Sheriff
Posts: 50189
79
Originally posted by GUNA RANJAN:
Campbell Ritchie please see you private message
Sorry if I was rude.

Campbell Ritchie
Sheriff
Posts: 50189
79
Yes, you have got it. Well done.

Matt Swaggi
Ranch Hand
Posts: 61
Thats all right sir ! i just said what i felt ! i am always frank !
thank you once again ! and i will correct my mistakes !

Paul Sisco
Greenhorn
Posts: 11
Originally posted by GUNA RANJAN:
Thank You all the way which i worked is that correct ! if not denote my mistakes !

Swapping three variable without using temp variable

a = 2 b = 1 c = 3

a = a+b+c = 6

b = a -b-c = 2
c = a -b- c = 1
a = a-b-c = 3

After swaping

a = 3 ; b = 2 ; c = 1

This looks like a neat trick, but is there a real world application for it? I don't think I have ever considered doing anything like this in any language.