A Program to Swap Dynamic Value Without 3rd Variable

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


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

  1. #include <stdio.h>
  2. #include <conio.h>
  3. int main() {
  4. printf("Enter the 1st value: ");
  5. scanf("%d", &a);
  6. printf("Enter the 2nd value: ");
  7. scanf("%d", &b);
  8. // code to swap 'a' and 'b'
  9. a = a + b;
  10. b = a - b;
  11. a = a - b;
  12. printf("After swapping: a = %d, b = %d", a, b);
  13. return 0;
  14. }

Output:

Enter the 1st value: 10
Enter the 2nd value: 20
After swapping: a = 20, b = 10