Swap Without Temp (XOR)
C
Easy
4 views
Problem Description
Swap two numbers using the SOR operator. Show step-by-step how the values are exchanged. Provide a mathematical proof
Official Solution
#include <stdio.h>
int main() {
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
printf("nBefore swap: a = %d, b = %dn", a, b);
/* XOR swapping (SOR) */
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!