swap without using third variable

Certainly! Here's an example of how to swap two variables without using a third variable in C:

#include <stdio.h>

int main() {
    int a = 5, b = 10;

    printf("Before swapping: a = %d, b = %d\n", a, b);

    a = a + b;
    b = a - b;
    a = a - b;

    printf("After swapping: a = %d, b = %d\n", a, b);

    return 0;
}

Explanation for each step:

  1. a = a + b; - Add the values of a and b and store the sum in variable a. Now, 'a' contains the sum of both numbers.
  2. b = a - b; - Subtract the value of b from the updated value of a. This result is stored in variable b, effectively assigning the original value of 'a' to 'b'.
  3. a = a - b; - Subtract the updated value of b from the updated value of a. This assigns the original value of 'b' to 'a', completing the swap.

This method leverages arithmetic operations to swap values without utilizing a third variable.