Change value of two variables without using a third, using only sum or subtraction [closed]

0

Using only two variables (A and B) and also using only (addition or subtraction), make the value of A become the value of B and the value of B becomes the value of A.

    
asked by anonymous 27.02.2018 / 17:39

1 answer

2

Can it be using the or-exclusive operator? If so:

#include <stdio.h>

int main() {
    int a = 123;
    int b = 456;
    printf("%d %d\n", a, b);
    a ^= b;
    b ^= a;
    a ^= b;
    printf("%d %d\n", a, b);
    return 0;
}

Here's the output:

123 456
456 123

See here working on ideone.

    
27.02.2018 / 17:44