Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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.