How do you swap two numbers without using a third variable in C by arithmatic operations? |
[Edit] |
Arithmatic swap without a 3rd variable
int iA = 5;
int iB = 6;
int iA = iA + iB; // iA is now 11
int iB = iA - iB; // iB is now 5
int iA = iA - iB; // iA is now 6
Note: Even though no overt variable has been created, the compiler has created temporary variables because a variable left of the equal sign has been used on the right.
First answer by ID1167588123. Last edit by ID1167588123. Question popularity: 9 [recommend question]
|
Research your answer: |
Can you answer other
questions about programming?



