0

Perform swapping of two numbers with the help of variables?

29th Nov 2016, 1:08 PM
munish rastogi
munish rastogi - avatar
2 Answers
+ 1
If you mean swapping the values of say A and B. If A = 20, and B = 10, you want to make B - 20, and A - 10 right? Example code: int A = 20; int B = 10; int Swap; Swap = A; A = B; B = Swap; The "Swap" is there to hold the value of A (or whatever value you are swapping), as once A is B, Saying "B = A" is just the same as "B = B" If you have any questions feel free to ask :)
29th Nov 2016, 2:07 PM
PenguinBlast
PenguinBlast - avatar
+ 1
Only two variables are required, if that is what you like. Example: int a = 5, b = 10; a += b; b = a - b; a -= b; //a = 10, b = 5
29th Nov 2016, 4:00 PM
Cohen Creber
Cohen Creber - avatar