Brain Teasers
Brain Teasers Trivia Mentalrobics Games Community
Personal Links
Submit a Teaser
Your Favorites
Your Watchlist
Browse Teasers
All

Cryptography
Group
Language
Letter-Equations
Logic
Logic-Grid
Math
Mystery
Optical-Illusions
Other
Probability
Rebus
Riddle
Science
Series
Situation
Trick
Trivia

Random
Daily Teasers
Search Teasers

Advanced Search
Add to Google Add to del.icio.us

More ways to get Braingle...

Variable Switch

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

 

Puzzle ID:#49422
Fun:** (1.88)
Difficulty:*** (2.29)
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.



Back to Top
   



Users in Chat : fishmed 

Online Now: 6 users and 466 guests

Copyright © 1999-2014 | Updates | FAQ | RSS | Widgets | Links | Green | Subscribe | Contact | Privacy | Conditions | Advertise

Custom Search





Sign In A Create a free account