A Program to Swap Fixed Value Without 3rd Variable

In this example, we are giving a program to swap fixed value without using the 3rd variable.


Program to to swap fixed value without the 3rd variable:

  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main() {
  4. int a = 10, b = 20;
  5. // code to swap 'a' and 'b'
  6. a = a + b; // a now becomes 30
  7. b = a - b; // b becomes 10 and getting new value of b
  8. a = a - b; // a becomes 20 and getting new value of a
  9. printf("After swapping: a = %d, b = %d", a, b);
  10. return 0;
  11. }

Output:

After swapping: a = 20, b = 10