Ask Your Question
0

How can you switch or interchange two variables without the use of an additional or temporary variable?

asked 2023-04-29 06:04:30 +0000

huitzilopochtli gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2023-04-29 06:08:01 +0000

plato gravatar image

You can switch or interchange two variables without the use of an additional or temporary variable by using arithmetic operations as follows:

  1. Add both variables and store the result in one of the variables. Example: a = a + b
  2. Subtract the value of one variable (which now contains the sum of both variables) from the other variable and store the result in the same variable. Example: b = a - b
  3. Subtract the value of one variable (which now contains the original value of the other variable) from the same variable and store the result in the other variable. Example: a = a - b

After these operations, the values of variables a and b will be interchanged without using any additional variables.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-04-29 06:04:30 +0000

Seen: 13 times

Last updated: Apr 29 '23