Swap Without Third Variable
C
Easy
3 views
Problem Description
Two integers need to be swapped without using a third variable. Use three different methods: arithmetic, XOR, and one more trick. Explain the logic behind each method.
Official Solution
#include <stdio.h>
int main() {
int a = 5, b = 10;
printf("Before Swap: a=%d, b=%dn", a, b);
a = a + b;
b = a - b;
a = a - b;
printf("After Swap: a=%d, b=%dn", a, b);
return 0;
}
Solutions (0)
No solutions submitted yet. Be the first!
No comments yet. Start the discussion!