Lets say we have two variables called a and b.Initialize these variables with some value.Now the objective here is to do some operations on these variables so that the values of the variables are swaped.

One operation that we can do here is addition and subtraction of the values stored in variables a and b.First we will add the values in the variables a and b and store in a.

1 |
a= a + b; |

a now contains the sum of the values in a and b.

Now we will subtract the value in b from a (which is total) .This will give us the value a (a+b-b).

1 |
b = a - b; |

We will again subtract the value b from a and assign to b (a + b – b)

1 2 3 4 5 6 |
int a,b ; a = 2; b = 4; a= a + b; b = a - b; a = a - b; |

## Leave a Reply