Brain Teasers Optical Illusions Puzzle Hunts Codes & Ciphers Mechanical Puzzles
Personal Links
Browse Teasers
Search Teasers

Variable Switch

Logic puzzles require you to think. You will have to be logical in your reasoning.

 

Puzzle ID:#49422
Fun:** (1.94)
Difficulty:*** (2.31)
Category:Logic
Submitted By:rob77*ca*****!

 

 

 



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.


Hint

You can do it in three steps.
Hide



What Next?

  
  

See another brain teaser just like this one...

Or, just get a random brain teaser

If you become a registered user you can vote on this brain teaser, keep track of
which ones you have seen, and even make your own.

 



Comments

royale-ninja
Jul 24, 2012

i got it...i solved it by another answer
A=B-A
B=B-A
A=A+B
royale-ninja
Jul 24, 2012

i got it...i solved it by another answer
A=B-A
B=B-A
A=A+B
edoridlo
Oct 14, 2013

How about this way?
A = A + B
B = A - B
A = A - B
rob77*ca*
Jan 08, 2014

This was a real exercise I got in a course.
Dezaxa
Dec 29, 2014

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

Back to Top
   


Online Now
10 users and 754 guests

Users In Chat
Aviator
Follow Braingle!
Fold 'N Fly Paper Airplanes
Easy to follow folding instructions and videos.
Copyright © 1999-2017 | FAQ | Widgets | Links | Green | Subscribe | Contact | Privacy | Conditions | Advertise | Braingle Time: 12:35 pm
Sign In Create a free account