File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes How will u swap three variable without using temp variable !?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How will u swap three variable without using temp variable !??" Watch "How will u swap three variable without using temp variable !??" New topic
Author

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

Matt Swaggi
Ranch Hand

Joined: May 29, 2008
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

Joined: Aug 16, 2005
Posts: 14268
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
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
Marshal

Joined: Mar 22, 2005
Posts: 42276
    
  64
Now that you know the way to swap two variables, maybe you extend the principle to three variables if you think about it?


Ping & DNS - my free Android networking tools app
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
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
Sheriff

Joined: Sep 28, 2004
Posts: 18896
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
Posts: 61
any practical example hendry ! ??
Charles Lyons
Author
Ranch Hand

Joined: Mar 27, 2003
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.


Charles Lyons (SCJP 1.4, April 2003; SCJP 5, Dec 2006; SCWCD 1.4b, April 2004)
Author of OCEJWCD Study Companion for Oracle Exam 1Z0-899 (ISBN 0955160340 / Amazon Amazon UK )
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
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

Joined: May 29, 2008
Posts: 61
Campbell Ritchie please see you private message
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
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

Joined: Oct 13, 2005
Posts: 39396
    
  28
Originally posted by GUNA RANJAN:
Campbell Ritchie please see you private message
Sorry if I was rude.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Yes, you have got it. Well done.
Matt Swaggi
Ranch Hand

Joined: May 29, 2008
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

Joined: Jun 15, 2008
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How will u swap three variable without using temp variable !??