Skip to main content

Swapping (Exchanging) of two numbers without using a third variable

#include <stdio.h>
int main()
{
  int a,b;
  printf("Enter two numbers ");
  scanf("%d%d", &a, &b );
  printf("\nBefore swapping, a = %d and b = %d", a, b);
  a = a + b;
  b = a - b;
  a = a - b;
  printf("\nAfter swapping, a=%d and b=%d", a, b);

  return 0;
}

Enter two numbers  7 9

Before swapping, a = 7 and b = 9
After swapping, a=9 and b=7 

Comments