
Lite 
Variable Switch
Category:  Logic 
Submitted By:  rob77 
Fun:  (1.94) 
Difficulty:  (2.31) 
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=BA to assign a new value to B.
Answer:
B = A+B
A = BA
B = BA
Using an example, if A=5 and B=4:
B = 5+4
A = 95
B = 94
A is now 4 and B is now 5.
Comments on this teaser
Most Popular  Hardest  Easiest
Privacy  Terms
Copyright © 2003