C Program to swap two numbers without using third variable


In this program, You will learn how to swap two numbers without using third variable in c.


Before swap: 10 20

After swap: 20 10

Example: How to swap two numbers without using third variable in c.

#include<stdio.h>

int main() {

  int a, b;

  printf("Enter two numbers:");
  scanf("%d%d", &a,&b);

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

  printf("After swapping value of a is:%d", a);
  printf("\nAfter swapping value of b is:%d", b);

  return 0;
}

Output:

Enter two numbers:10 20                                                                                                                                             
After swapping value of a is:20                                                                                                                                     
After swapping value of b is:10