0
Perform swapping of two numbers with the help of variables?
2 Antworten
+ 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 :)
+ 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