Braingle Lite



Variable Switch

Category:Logic
Submitted By:rob77
Fun:** (1.94)
Difficulty:*** (2.28)



You're a computer programmer, and the program you're writing has numeric variables A and B. You need the computer to switch the variables so that the number in variable A becomes variable B, and the number in variable B becomes variable A. Adding a third variable would use more memory though, so how can you do that switch without using another variable? Use commands such as B=B-A to assign a new value to B.

Show Hint Show Answer



Comments on this teaser


Posted by royale-ninja07/24/12
i got it...i solved it by another answer A=B-A B=B-A A=A+B

Posted by royale-ninja07/24/12
i got it...i solved it by another answer A=B-A B=B-A A=A+B :D

Posted by edoridlo10/14/13
How about this way? A = A + B B = A - B A = A - B :D

Posted by rob7701/08/14
This was a real exercise I got in a course.

Posted by Dezaxa12/29/14
The smart alec answer is to emply some assembler and use registers, e.g. load location of A to register X load location of B to register Y save X to location of B save Y to location of A




Search:
Browse:

Most Popular | Hardest | Easiest




Privacy | Terms
Copyright © 2003